javascript - jQuery ways to dynamically .append a div to the dom, and then find it later without setting an ID, class, or anyway to indentify it -


update: contributed, it's appreciated, kind , generous , of deserve dear respect. cheers.

note: i'm making simple jquery tooltip plugin, tooltip fire on mouseover. mouseover create instance of div tool-tip specific each anchor launched div tool-tip. each anchor class .c_tool have own created div erase after mouseout. anyway details irrelevant. important how create div .append() or .add() on , find way call , apply actions div without setting identifier (id), class, or means identify it.

i know theres way find div counting, if gave every created div same class , counted them find one, don't know if efficient method why i'm asking help.

i'm not going post whole plugin script thats unnecessary, i'll paste simplified version.

 <a href="#" class="c_tool">hover me</a> <a href="#" class="c_tool">hover me</a>  $(document).ready(function() {      obj = $('a.c_tool');     obj.mouseover(function() {          /// append div body specific each item class c_tool, don't want set id, or class appended div      }).mouseout(function() {          /// remove added div without setting id or class it.     });  }); 

to create new dom node can use jquery constructor, like

$(document).ready(function() {     obj = $('a.c_tool');      obj.mouseover(function() {         if(!$.data(this, 'ref')) {             $.data(this, 'ref', $ref = $('<div>', {                  html:   'hello world!'             }).appendto(document.body));         }     }).mouseout(function() {         $.data(this, 'ref').remove();     }); }); 

.appendto() returns dom node of invocation (in case, newly created div) jquery object. way can store reference in variable instance , access later.

referring comment:
remove stored references, should this:

$('a.c_tool').each(function(index, node) {     $.removedata(node, 'ref'); }); 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -