Why can't .attr()
change the value of value
?
$("input").attr("value") = "New text";
See also a live jsfiddle example
In general: Because the value attribute sets the default value, not the current value. Use the val()
method instead.
$("input").val("New text");
In your specific case (because the value hasn't be changed by the user): because the attr
method doesn't give you something you can assign a value to (and can't be written in such a way as would allow you to). It takes two arguments, the second of which is the value you want to assign.
$("input").attr("value", "New text");
Replace $("input").attr("value") = "New text";
with $("input").attr("value","New text");
attr( attributeName, value )
That is the proper signature for attr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With