javascript - How to select an element that was created in a for loop? -


I have loaded lots of elements on my page dynamically in each list, one descriptor and one in each list A highlighter is overlaid on the image.

What do I want when a user enters the list item, the other element is highlighted.

My current code only highlights the last element in .viewer , no matter what list item I run.

What is the best way to handle it? for (i = 0; i & lt; Ajax lang; ++ i) {var listid = 'listitem -' + i; Var mapad = 'mapitim -'; $ ('.list'). App ('& lt; div id = "' + listid + '" & gt; lots of text & lt; / div & gt;'); $ ('. Viewer') Attachment ('& lt; div id = "' + + + + '+'"> here it is ;); $ ('#' + Listid). Hover (function () {$ ('#' + mapid) .toggleClass ('Highlight');}); }

the last value assigned by it in Hover Time removed for event handlers, so you have to make sure that you have relevant values.

Try putting the hover assignment inside the horse ... (I = 0; i & lt; Ajax; long; ++ i) { var listid = 'listitem -' + i; Var mapad = 'mapitim -'; $ ('.list'). App ('& lt; div id = "' + listid + '" & gt; lots of text & lt; / div & gt;'); $ ('. Viewer') Attachment ('& lt; div id = "' + + + + '+'"> here it is ;); (Function (Listed, Mapped) {$ ('#' + listid). Hover (function () {$ ('#' + Mapped). Togglic ('Highlight');})}}) (Listed, Mapad); }

An optional method must be stored with the list item associated with the list item, such as ...

 for  (i = 0; i & Lt; Ajax.length; ++ i) {var listid = 'list -' + i; Var mapad = 'mapitim -'; $ ('.list'). App ('& lt; div id = "' + listid + '" & gt; lots of text & lt; / div & gt;'); $ ('. Viewer') Attachment ('& lt; div id = "' + + + + '+'"> here it is ;); $ ('#' + Listid) .data ("affiliate-map-id", mapbed); $ ('#' + Listid). Hover (function () {$ ('#' + $ (this) .data ("affiliate-map-id")). Toggleclass ('highlight');}); }   

Either work and they will give the same end result just choose what you like :)

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -