Basically when invoking .attr("value")
over a text box, It will return its value which was set in the markup, not the value which was set by using .val()
.
For instance,
<input id="test" type="text" value="testing" />
Js:
$("#test").val("hello");
console.log($("#test").val()); //hello
console.log($("#test").attr('value')); //testing
But while doing the same over a hidden element, the result was different like below,
HTML:
<input id="test1" type="hidden" value="testing" />
Js:
$("#test1").val("hello");
console.log($("#test1").val()); //hello
console.log($("#test1").attr('value')); //hello
DEMO
The value attribute got ignored if we set value for that element by using .val()
. Anyone have idea on why is this happening? Relevant link which contains details for this behavior would be more helpful.