Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using jQuery, just use a selector instead of <code>getElementById()</code> and use <code>append()</code> to add content to it.</p> <p>Also, you should never have two items with the same ID. You can sort it out by appending your <code>famElements</code> counter to the IDs of your input fields. As this change ruled out selecting the elements by ID, I also changed your checkboxes to have a class <code>cit</code>, so I can attach the event using an easy selector.</p> <p>For the click event, you can use <a href="http://api.jquery.com/live/" rel="nofollow"><code>.live()</code></a> or <a href="http://api.jquery.com/delegate/" rel="nofollow"><code>.delegate()</code></a> so you don't have to keep binding events all the time.</p> <pre><code>function familySize() { while (famElements &lt; $("#famNum").val()) { // add fields famElements++; addFields = '&lt;div class="member"&gt; Name:&amp;nbsp;&lt;input id="name'+famElements+'" type="text" placeholder="Name" required&gt; &amp;nbsp;&amp;nbsp;&lt;input class="cit" id="cit'+famElements+'" type="checkbox" onChange="citizenInfo()" &gt;&amp;nbsp;&amp;nbsp;Non U.S. Citizen?&lt;br/&gt;&lt;/div&gt; &lt;div class="citInfo" style="display:none"&gt; &lt;input id="countryCitizenship'+famElements+'" type="text" placeholder="Citizenship" required&gt;&lt;/div&gt;'; $('#famItems').append(addFields); } while (famElements &gt; $("#famNum").val()) { // remove fields console.log("remove one"); $('#famItems div:last').remove(); famElements--; } } // Your jQuery ready function $(function(){ // Click event on checkboxes $('#famItems').delegate('.cit','click',function(){ var $this = $(this); // Show or hide the div if($this.is(':checked')){ $this.parent().find('.citInfo').hide(); } else { $this.parent().find('.citInfo').show(); } }); }); </code></pre> <p>You can build your elements more elegantly with jQuery, but it does what it needs to do. Although you might consider doing that in case you add to your HTML and that big lump of text becomes too much of a mystery.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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