Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can i get id/ generate id of dynamically generated elements in html using jquery?
    text
    copied!<p>Hi i have a student profile form where student can fill their details. Student can add school, add high school and add college dynamically by clicking add school, add high school and add college anchor tag.</p> <p><img src="https://i.stack.imgur.com/TZXGq.png" alt="Student Profile"></p> <p>Code for this form is as below</p> <pre><code> &lt;script type="text/javascript"&gt; $(document).ready(function () { $('.studentprofileupdateBody a').click(function () { console.log('ok'); var $tr = $(this).closest('tr').prev() var clone = $tr.clone(); $tr.after(clone); }); }); &lt;/script&gt; &lt;form method="post" action=""&gt; &lt;table width="100%"&gt; &lt;tr&gt; &lt;td&gt; &lt;div id="Education"&gt; &lt;table class="Studentprofile" id="tblEducation"&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt; Education Details&lt;/td&gt; &lt;/tr&gt; &lt;tr id="schoolRow" name="schoolRow"&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;label for="schools"&gt;Schools:&lt;/label&gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody" id="schools"&gt; &lt;input type="text" id="s1" name="s1" title="S1"&gt;&lt;/input&gt; &lt;select name="selSYear"&gt; &lt;option value="0"&gt;-Select-&lt;/option&gt; &lt;option value="1"&gt;2010&lt;/option&gt; &lt;option value="2"&gt;2009&lt;/option&gt; &lt;option value="3"&gt;2008&lt;/option&gt; &lt;option value="4"&gt;2007&lt;/option&gt; &lt;option value="5"&gt;2006&lt;/option&gt; &lt;option value="6"&gt;2005&lt;/option&gt; &lt;option value="7"&gt;2004&lt;/option&gt; &lt;option value="8"&gt;2001&lt;/option&gt; &lt;option value="8"&gt;2000&lt;/option&gt; &lt;//select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;a href="#"&gt;Add Schools&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;hr class="profileUpdate"/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="highschoolRow" name="highschoolRow"&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;label for="highschool"&gt;High School:&lt;/label&gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody" id="highschool"&gt; &lt;input type="text" id="hs1" name="hs11" title="HS1" /&gt; &lt;select name="selHSYear"&gt; &lt;option value="0"&gt;-Select-&lt;/option&gt; &lt;option value="1"&gt;2010&lt;/option&gt; &lt;option value="2"&gt;2009&lt;/option&gt; &lt;option value="3"&gt;2008&lt;/option&gt; &lt;option value="4"&gt;2007&lt;/option&gt; &lt;option value="5"&gt;2006&lt;/option&gt; &lt;option value="6"&gt;2005&lt;/option&gt; &lt;option value="7"&gt;2004&lt;/option&gt; &lt;option value="8"&gt;2001&lt;/option&gt; &lt;option value="8"&gt;2000&lt;/option&gt; &lt;//select&gt;&gt; &lt;br&gt;&lt;br/&gt; &lt;hr class="profileSubSection"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;a href="#"&gt;Add High Schools&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;hr class="profileUpdate"/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="collegeRow" name="collegeRow"&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;label for="college"&gt;College:&lt;/label&gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody" id="college"&gt; &lt;input type="text" id="co1" name="co1" title="CO1"/&gt; &lt;select name="selColYear"&gt; &lt;option value="0"&gt;-Select-&lt;/option&gt; &lt;option value="1"&gt;2010&lt;/option&gt; &lt;option value="2"&gt;2009&lt;/option&gt; &lt;option value="3"&gt;2008&lt;/option&gt; &lt;option value="4"&gt;2007&lt;/option&gt; &lt;option value="5"&gt;2006&lt;/option&gt; &lt;option value="6"&gt;2005&lt;/option&gt; &lt;option value="7"&gt;2004&lt;/option&gt; &lt;option value="8"&gt;2001&lt;/option&gt; &lt;option value="8"&gt;2000&lt;/option&gt; &lt;//select&gt; &lt;br&gt;&lt;br/&gt; &lt;hr class="profileSubSection"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;a href="#"&gt;Add College&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;hr class="profileUpdate"/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;input type="submit" id="saveStudentEducationInfo" name="saveStudentEducationInfo" title="Save Education Info" value="Save Details" /&gt; &lt;input type="submit" id="cancelStudentEducationInfo" name="cancelStudentEducationInfo" title="Cancel Education Info" value="Cancel" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>It works perfect and add any no of elements dynamically but when i see the page source of this page i get to know that its not generating new elements id.</p> <p>It looks like this..</p> <p><img src="https://i.stack.imgur.com/3Vmbm.jpg" alt="enter image description here"></p> <p>Generated New elements but by viewing source code of this page it gives,</p> <pre><code> &lt;form method="post" action=""&gt; &lt;table width="100%"&gt; &lt;tr&gt; &lt;td&gt; &lt;div id="Education"&gt; &lt;table class="Studentprofile" id="tblEducation"&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt; Education Details&lt;/td&gt; &lt;/tr&gt; &lt;tr id="schoolRow" name="schoolRow"&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;label for="schools"&gt;Schools:&lt;/label&gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody" id="schools"&gt; &lt;input type="text" id="s1" name="s1" title="S1"&gt;&lt;/input&gt; &lt;select name="selSYear"&gt; &lt;option value="0"&gt;-Select-&lt;/option&gt; &lt;option value="1"&gt;2010&lt;/option&gt; &lt;option value="2"&gt;2009&lt;/option&gt; &lt;option value="3"&gt;2008&lt;/option&gt; &lt;option value="4"&gt;2007&lt;/option&gt; &lt;option value="5"&gt;2006&lt;/option&gt; &lt;option value="6"&gt;2005&lt;/option&gt; &lt;option value="7"&gt;2004&lt;/option&gt; &lt;option value="8"&gt;2001&lt;/option&gt; &lt;option value="8"&gt;2000&lt;/option&gt; &lt;//select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;a href="#"&gt;Add Schools&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;hr class="profileUpdate"/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="highschoolRow" name="highschoolRow"&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;label for="highschool"&gt;High School:&lt;/label&gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody" id="highschool"&gt; &lt;input type="text" id="hs1" name="hs11" title="HS1" /&gt; &lt;select name="selHSYear"&gt; &lt;option value="0"&gt;-Select-&lt;/option&gt; &lt;option value="1"&gt;2010&lt;/option&gt; &lt;option value="2"&gt;2009&lt;/option&gt; &lt;option value="3"&gt;2008&lt;/option&gt; &lt;option value="4"&gt;2007&lt;/option&gt; &lt;option value="5"&gt;2006&lt;/option&gt; &lt;option value="6"&gt;2005&lt;/option&gt; &lt;option value="7"&gt;2004&lt;/option&gt; &lt;option value="8"&gt;2001&lt;/option&gt; &lt;option value="8"&gt;2000&lt;/option&gt; &lt;//select&gt;&gt; &lt;br&gt;&lt;br/&gt; &lt;hr class="profileSubSection"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;a href="#"&gt;Add High Schools&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;hr class="profileUpdate"/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr id="collegeRow" name="collegeRow"&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;label for="college"&gt;College:&lt;/label&gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody" id="college"&gt; &lt;input type="text" id="co1" name="co1" title="CO1"/&gt; &lt;select name="selColYear"&gt; &lt;option value="0"&gt;-Select-&lt;/option&gt; &lt;option value="1"&gt;2010&lt;/option&gt; &lt;option value="2"&gt;2009&lt;/option&gt; &lt;option value="3"&gt;2008&lt;/option&gt; &lt;option value="4"&gt;2007&lt;/option&gt; &lt;option value="5"&gt;2006&lt;/option&gt; &lt;option value="6"&gt;2005&lt;/option&gt; &lt;option value="7"&gt;2004&lt;/option&gt; &lt;option value="8"&gt;2001&lt;/option&gt; &lt;option value="8"&gt;2000&lt;/option&gt; &lt;//select&gt; &lt;br&gt;&lt;br/&gt; &lt;hr class="profileSubSection"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;a href="#"&gt;Add College&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;hr class="profileUpdate"/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class="studentprofileupdateHead" &gt; &lt;/td&gt; &lt;td class="studentprofileupdateBody"&gt; &lt;input type="submit" id="saveStudentEducationInfo" name="saveStudentEducationInfo" title="Save Education Info" value="Save Details" /&gt; &lt;input type="submit" id="cancelStudentEducationInfo" name="cancelStudentEducationInfo" title="Cancel Education Info" value="Cancel" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;br/&gt;&lt;br/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>I want my code to generate new id for each element to distinctly identify each element by its id.</p> <p>How can i get id for each element?</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