Reputation: 79
When I want to read an attribute, I use
$('#mydiv').attr('id');
How do I write to this attribute instead, so that I can change the id of the div in the markup?
Upvotes: 0
Views: 180
Reputation: 324547
It's more efficient and more cross-browser compatible just to use the element's id
property:
$("#mydiv")[0].id = "newId";
Even better, you could cut out jQuery altogether:
document.getElementById("mydiv").id = "newId";
Upvotes: 1
Reputation: 7670
Include second parameter in attr call with value to set to.
$('#mydiv').attr('id', 'value');
Upvotes: 0
Reputation:
$('#mydiv').attr('id', 'value');
take a look at the jquery documentation, http://api.jquery.com/attr/ it's well explained
Upvotes: 4