title: Javascript - remove specific element from dynamically created array
body: <p>I have a page where users can create tags (much like here in stackoverflow), which are then sent(POST) to the back end to be stored in a database. The user can make tags but also remove them before finally hitting Submit.</p>
  
  <p>In the DOM the tags are generated along with an 'x' button. The 'x' button removes the element from the DOM, but the trouble comes when removing from the array. The closest I could get to a solution was <a href="https://stackoverflow.com/questions/1275190/javascript-arrays-removing-array-elements">this question</a>, however I couldn't get it to quite work for me.</p>
  
  <p><a href="http://codepen.io/MartinBort/pen/hoscE" rel="nofollow noreferrer">Here's the codepen</a></p>
  
  <p>Here's the javascript (i'm using JQuery)</p>
  
  <pre><code>window.tag_array = [];
  
  $( "#addtag" ).click(function() {
  
  var tag = $("#input-tag").val();
  
  //if tag is empty
  if(!$('#input-tag').val()) {
  
      alert("can't be empty");
  
      } else {
          //put tag.val into an array         
          tag_array.push(tag);
  
          //add to DOM
          $( "#tagsbox" )
          .append( "&lt;div class='displaytag'&gt;&lt;i&gt;"+tag+"&lt;/i&gt;&lt;input type='hidden' class='tag' value="+tag+"&gt;&lt;button onClick='return false;' class='removetag'&gt;x&lt;/button&gt;&lt;/div&gt;" );
  
          //reset value in text area to null
          $("#input-tag").val("");
  
          //remove tag onclick
          $('.removetag').click(function() {
              $(this).parent().remove(); //remove tag from DOM
  
              //splice from array
              tag_array.splice( this, 1 ); //&lt;--HERE IS PROBLEM (i think)
  
          });
  
  
      } //end else
  
      alert(tag_array); //check array
  });
  </code></pre>
  
  <p>The end result is the splice takes out too many array items.</p>
  
  <p>I have also tried</p>
  
  <pre><code>tag_array.splice(tag_array.indexOf(tag),1);
  </code></pre>
  
  <p>to a similar result.</p>
  
  <p>Please help! Thanks in advance</p>