I need to change an element’s id when I mouseover a different element in a particular class. I then want to change the element id back to what it was previously on mouseout from the same element that I moused over before.
In the code below I propound a basic HTML code to clarify my question. I wish to change <div id="Set1"> to <div id="Set2"> onmouseover of any <div class="tomouseover">. Onmouseout from that same element I wish to change the original element’s id back to <div id="Set1">.
<div id="Set1">Div to change id</div>
<div class="tomouseover">Mouse over to change Set1 to Set2</div>
<div class="tomouseover">Mouse over to change Set1 to Set2</div>
+1 to ANY ideas! I’m royally stuck…
You can try Derek’s answer but you will soon discover that querySelector is not supported by IE8 in quirks mode or IE 7 and lower, and that addEventListener is not supprted by IE 8 and lower at all.
Choices for plain js include writing your own getElementsByClassname function (not particularly difficult) or use event delegation so that you attach only one listener for each event and need a simple hasClass function.
Anyhow, here’s an example:
Some standard library functions that any javscripter should have in a library or be able to code in a few minutes:
A function to do the work:
The above will work in all browsers back to IE6 and NN 3 or so and will continue to work in future browsers that are either W3C or IE event models.