Tom Tom - 4 months ago 14
Javascript Question

JQuery Find #ID, RemoveClass and AddClass

I have the following HTML

<div id="testID" class="test1">
<img id="testID2" class="test2" alt="" src="some-image.gif" />

I basically want to get to #testID2 and replace .test2 class with .test3 class ?

I tried


But this doesn't appear to work ?

Any ideas ?


Semantically, you are selecting the element with the ID testID2, then you are looking for any descendent elements with the class test2 (does not exist) and then you are replacing that element with another element (elements anywhere in the page with the class test3) that also do not exist.

You need to do this:


This selects the element with the ID testID2, then adds the class test3 to it. Last, it removes the class test2 from that element.