title: Javascript & Html loads twice (form field) how can I know which element it belongs to?
body: <p><strong>HTML</strong>:</p>
  
  <p>The field below is being loaded dynamically, more than once.</p>
  
  <pre><code>&lt;div&gt;
  &lt;label&gt;&lt;?php echo $form-&gt;label; ?&gt;
  &lt;select 
         name=&lt;?php echo $form-&gt;name; ?&gt;
         idx=&lt;?php echo $form-&gt;entity; ?&gt;
  &gt;
  &lt;option value="&lt;?php echo $form-&gt;option()-&gt;value; ?&gt;"&gt;&lt;?php echo $form-&gt;option()-&gt;name; ?&gt;&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;td&gt;
  &lt;a href="#" id="addnew"  onclick="onOffButton('addfield', 'addbtn'); return false;"&gt;[Open] Add New&lt;/a&gt;
  
  &lt;span id="addfield"&gt;
     &lt;input name="newstuff type="text"&gt;&lt;/input&gt;
  &lt;/span&gt;
  
  &lt;a href="#do-something" id="addbtn"&gt;Click to add&lt;/a&gt;
  &lt;/td&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>JAVASCRIPT</strong>:</p>
  
  <p>I can make the script below load twice, or once. If I load it once, then it only works for the first field. If I load it twice, then it only works for the second field. </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      var onix = false;
      function onOffButton(id, text) {
  
          var select_id = $("#addfield").prevAll('select').attr("idx");
          console.log(select_id);
  
          if (onix == false) {
              document.getElementById(id).style.display = 'block';
              document.getElementById(text).style.display = 'block';
              document.getElementById("addnew").textContent="[Close] Add New";
              onix = true;                   
          } else if (onix == true) {
              document.getElementById(id).style.display = 'none';
              document.getElementById(text).style.display = 'none';
              document.getElementById("addnew").textContent="[Open] Add New";
              onix = false;                
          }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>SUMMARY</strong></p>
  
  <p>I need to know which element the section addfield belongs to. Most importantly, the form->entity. The goal is to generate a GET link like so: <code>localhost/$form-&gt;entity/create/[value-of-newstuff]</code></p>
  
  <p><strong>I tried</strong> setting an array on an external file. And that does work, but I don't know what to do with it.</p>
  
  <p><strong>I also tried</strong> using <code>&lt;?php echo rand(); ?&gt;</code> on all of the attributes. The results were as expected, when javascript loads once it only works for the first field, loads twice, only works for the second field.</p>
  
  <p>How can I overcome this obstacle?</p>