Home › Forums › JavaScript › Changing ID of a div with jquery
- This topic is empty.
-
AuthorPosts
-
March 3, 2012 at 6:46 pm #36966jgessentialsMember
Hi
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 #98073SenffParticipantI 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 #98081jgessentialsMemberWonderful, how would I go about changing the classes of the div through links?
March 3, 2012 at 9:14 pm #98083SenffParticipantTo 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 #98084jgessentialsMemberMarch 3, 2012 at 11:50 pm #98086jgessentialsMemberOh that looks nice, thank you!! So what would my 3 links look like?
March 4, 2012 at 9:24 am #98114jgessentialsMemberThank 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 #98117jgessentialsMemberGot it!
http://jsfiddle.net/cunj2/4/
March 4, 2012 at 5:01 pm #98129VermaasParticipantThis 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 #98189jgessentialsMemberVery nice that is much better thank you!!
June 17, 2015 at 11:50 am #203938RedishParticipantHi 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.