Reputation: 59491
How to change the id
of the div
using jquery?
I have div
with id
(initially as 'first') and a dropdown (with values 'first', 'second').
On changing the dropdown value, i need to change the id of the div
according to dropdown value.
Upvotes: 4
Views: 13864
Reputation: 101
I'd rather use the title attribute for those things. id might as well be untouchable. Essentially:
your_element.attr("title", YOUR_TITLE);
Upvotes: 0
Reputation: 19627
Are you sure you want to do that?
Keep in mind the meaning of 'identity'. The ID of an element is meant to be a unique identifier, not just any old attribute.
If you find yourself in a position where you need to change the ID, perhaps your thinking is skewed somehow. It smells ;-)
Upvotes: 3
Reputation: 474
It is simple:
$('#first').attr('id', 'second');
Add it to your onchange function.
Upvotes: 11
Reputation: 342635
$('select').change(function() {
$('div.something').attr('id',$(this).val());
});
You'll need to use a non-ID selector to get the DIV with the ID you want to change. In the above example I have assumed a class called something
.
Is there any particular reason you want to change the id of the div? I'm sure there's a better solution. Could you elaborate a bit more?
Upvotes: 6