5

Why can't .attr() change the value of value?

$("input").attr("value") = "New text";

See also a live jsfiddle example

4 Answers 4

22

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");
Sign up to request clarification or add additional context in comments.

Comments

3

Replace $("input").attr("value") = "New text";

with $("input").attr("value","New text");

attr( attributeName, value )

That is the proper signature for attr

Comments

1

You should do

$("input").val("New text");

Comments

0

This code may help you :

 $("input").attr(attributName , valueYouWantToGiveToTheAttribute)

The attr function first parameter is the attribute name and the second option the value that you want to give to the attribute. Via this function, you can add attributes to the element and also edit attributes value of an element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.