Home › Forums › JavaScript › Changing ID of a div with jquery
- This topic is empty.
-
AuthorPosts
-
March 3, 2012 at 6:46 pm #36966
jgessentials
MemberHi
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=3Div
image changesDiv
text changesI 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 changeAny hep would be highly appreciated
Thank you!March 3, 2012 at 8:01 pm #98073Senff
ParticipantI 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 #98081jgessentials
MemberWonderful, how would I go about changing the classes of the div through links?
March 3, 2012 at 9:14 pm #98083Senff
ParticipantTo add a class “-status-on” to an element that has id “blah-01”:
$('#blah-01').addClass('status-on');
To remove this class:
$('#blah-01').removeClass('status-on');
To add a class and remove another one:
$('#blah-01').removeClass('status-on').addClass('status-off');
March 3, 2012 at 9:30 pm #98084jgessentials
MemberMarch 3, 2012 at 11:50 pm #98086jgessentials
MemberOh that looks nice, thank you!! So what would my 3 links look like?
March 4, 2012 at 9:24 am #98114jgessentials
MemberThank you :)
In your example, it is when you click the text that the links get the new class and change color.http://jsfiddle.net/cunj2/
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 #98117jgessentials
MemberGot it!
http://jsfiddle.net/cunj2/4/
March 4, 2012 at 5:01 pm #98129Vermaas
ParticipantThis 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 #98189jgessentials
MemberVery nice that is much better thank you!!
June 17, 2015 at 11:50 am #203938Redish
ParticipantHi 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?
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.