views:

663

answers:

3

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.

+1  A: 
$('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?

karim79
i like to change the theme of the page using this. All my content will be inside a particular div. and i have a css defined for that div. on changing dropdown value, i' like to change the entire page UI appearance
Prasad
I tried like this and it worked. $('select:selecttheme').change(function() { $('div[name="themecontent"]').attr('id', $(this).val()); }
Prasad
+1  A: 

It is simple:

$('#first').attr('id', 'second');

Add it to your onchange function.

Arkadiusz Kondas
why vote down ? I try it and it works !
Arkadiusz Kondas
After changing the first id, how you will change it again. You have to store old id somewhere, but thats a bad solution.
pokrate
i know that, but this is anserw to the question that his ask
Arkadiusz Kondas
@Arkadiusz: I agree, and voted you back up :-)
Tor Haugen
+2  A: 

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 ;-)

Tor Haugen