Prasad
Prasad

Reputation: 59491

change div id using jquery

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

Answers (4)

Léster
Léster

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

Tor Haugen
Tor Haugen

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

Arkadiusz Kondas
Arkadiusz Kondas

Reputation: 474

It is simple:

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

Add it to your onchange function.

Upvotes: 11

karim79
karim79

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

Related Questions