- This topic is empty.
March 3, 2012 at 6:46 pm #36966
Is there a simple way to change the id of a div by clicking on a link?
Here is what I am going for:
Link1 gives div id=1
Link2 gives div id=2
Link3 gives div id=3
I am trying to make both an image and text change by clicking on the appropriate link.
I figured I could change the id of div with the image and use a sprite to get the right part of the image to show up
I would also like the text in the second div to change
Any hep would be highly appreciated
Thank you!March 3, 2012 at 8:01 pm #98073SenffParticipant
I would go with assigning (and changing) unique classes to those DIVs and target that. To change the ID of a DIV is, in my opinion, defying the purpose of having IDs in the first place.
Adding and removing classes is a breeze in jQuery.March 3, 2012 at 9:04 pm #98081
Wonderful, how would I go about changing the classes of the div through links?March 3, 2012 at 9:14 pm #98083SenffParticipant
To add a class “-status-on” to an element that has id “blah-01”:
To remove this class:
To add a class and remove another one:
$('#blah-01').removeClass('status-on').addClass('status-off');March 3, 2012 at 9:30 pm #98084March 3, 2012 at 11:50 pm #98086
Oh that looks nice, thank you!! So what would my 3 links look like?March 4, 2012 at 9:24 am #98114
Thank you :)
In your example, it is when you click the text that the links get the new class and change color.
I will try to work with it so that when you click the links the text changes color ie gets the right class according to the link that was clicked.
Thank you for sharing the code with me and all your help.March 4, 2012 at 10:31 am #98117
http://jsfiddle.net/cunj2/4/March 4, 2012 at 5:01 pm #98129VermaasParticipant
This will even work better: http://jsfiddle.net/savver/ZbXNa/
On this way you can select a color multiple times and you can easily re-use this piece of code. On this way the only thing you need to do is add some new css classes to your stylesheet and make sure everything has the right id ;)!
Enjoy it!March 5, 2012 at 1:31 pm #98189
Very nice that is much better thank you!!June 17, 2015 at 11:50 am #203938RedishParticipant
Hi Vermaas, I’m looking for this topic, unfortunately the link with your solution it’s broken. There’s any chance to have the code again?