如何使用onChange函数获取文本框的值

I want to get a value of text box when it's value chenaged. I am using onchange function to get the value of text box when it changed.Here it's give a result but not changed value.It give's a value of the text box before changed value.How can get changed value.

Here is my script:

function myFunction(id)
{
    var id=id;
    var quantity = document.getElementById("quantity").value;
    alert(quantity);
} 

My onchange function:

<input type="text" value="<?php echo $res['quantity']; ?>" name="qty" id="quantity"  onChange="myFunction(<?php echo $res['id']; ?>);"/>

Thanks in advance.

For <input> tag you have oninput event, not onchange. Your Javascript should look like this:

document.querySelector('#quantity').addEventListener('input', myFunction);

function myFunction() {
    // 'this'  refers to the input, because you appended listener to it
    console.log(this.value); //to avoid alert spam...
} 

Functions gets hoisted so you can use them before they are declared, in case you were wondering.

Another way to add listener (but beware, you can replace or override it if you perform assignment again:

document.querySelector('#quantity').oninput = myFunction;

And your HTML would look like this:

<input type='text' id='quantity' name='qty' value="<?php echo $res['quantity']; ?>">

Use OnKeyUp or blur Event so once you enter text and switch to something from that text box you will get that text use alert to have a check:

    <input type="text" onBlur="check(this.value);" />

    Function check(id) {
    alert(id);
    }

hope it will help...