Exploit
Exploit

Reputation: 6386

how to change the value of a attribute inside a div with jquery

here is the div

<div class="rating" data=""></div>

what i'm wondering is how i can change the value of data to say the number 5

would something like this work? or its done another way?

$(".rating").attr("data").val("5");

Upvotes: 0

Views: 291

Answers (3)

Dancrumb
Dancrumb

Reputation: 27529

Adhering literally to your question, the right code would be:

 $(".rating").attr("data", "5");

However, data is not a valid HTML attribute in anything other than HTML 5.

You might want to consider the jQuery data method instead, for code that is not coupled to the latest version of HTML.

Upvotes: 3

Kevin Ennis
Kevin Ennis

Reputation: 14456

$(".rating").attr("data", "5");

Upvotes: 5

Gary Green
Gary Green

Reputation: 22395

$(".rating").attr("data", "5");

Upvotes: 3

Related Questions