sarimoto
sarimoto

Reputation: 79

How to SET jquery .attr(), not get it

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

Answers (4)

Tim Down
Tim Down

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

kobe
kobe

Reputation: 15835

$('#mydiv').attr('id',value);

Upvotes: 0

Sean
Sean

Reputation: 7670

Include second parameter in attr call with value to set to.

$('#mydiv').attr('id', 'value');

Upvotes: 0

fcalderan
fcalderan

Reputation:

$('#mydiv').attr('id', 'value');

take a look at the jquery documentation, http://api.jquery.com/attr/ it's well explained

Upvotes: 4

Related Questions