Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting for existing element with jQuery's .length doesn't see newly created elements
    text
    copied!<p>I'm trying to create a copy/paste system for a HTML table using jQuery and a context menu plugin and I'm trying to uniquely name all of the newly created rows. So I have a function that is cloning the selected row and inserting the new row above:</p> <pre><code>function cloneAbove(TR) { var newRow = $(TR).clone(); var lastID = $(TR).attr('id'); var newID = Number(lastID.substring(3))-0.1; //See if that row already exists: if($('#tr_'+newID).length){ alert('#tr_'+newID+' Exists'); //If it does exist, we divide the newID by 10 until we find one that doesn't: var i = 0; while(i &lt; 1){ newID = newID/10; if($('#tr_'+newID).length &gt; 0){ i = 1; } } } $(newRow).attr('id','tr_'+newID); $(TR).before(newRow); $(".target").contextmenu(option); } </code></pre> <p>First it clones the selected row (ie: '#tr_1'), subtracts 0.1 for the new row's id (ie: '#tr_0.9'), then it is supposed to check to see if that id already exists - this is where my problem is - if it exists it enters a loop to divide by 10 until it finds a id that doesn't.</p> <p>Here is a sample of the table:</p> <pre><code>&lt;table id="table" border=1&gt; &lt;tr class="target" id="tr_1" oncontextmenu="context('tr1')"&gt; &lt;td id="tr_1_1"&gt;Row1&lt;/td&gt; &lt;td id="tr_1_2"&gt;Row1&lt;/td&gt; &lt;/tr&gt; &lt;tr class="target" id="tr_2" oncontextmenu="context('tr2')"&gt; &lt;td id="tr_2_1"&gt;Row2&lt;/td&gt; &lt;td id="tr_2_2"&gt;Row2&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>The .length works for the 'hard coded' elements as they exist when the page loads, but it won't detect any of the newly created elements. Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload