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.
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.
$('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?
It is simple:
$('#first').attr('id', 'second');
Add it to your onchange function.
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 ;-)