title: jQuery Hide / Show input when selected option is equal to
body: <p>If selected option is equal to 2, I want to add the following line to the body:</p>
  
  <pre><code> &lt;input id="fnivel2" pk="1"/&gt;
  </code></pre>
  
  <p>I did it using a hide class. It works but I'm not sure if it is a correct solution:</p>
  
  <pre><code>&lt;script&gt;
  $(document).ready(function (){
      $("#fnivel").change(function(){
      var selected_option = $('#fnivel').val();
      if(selected_option == '2'){
          $("#fnivel2").removeClass("hide");
          $("#fnivel2").attr('pk','1');
      }
          if(selected_option != '2'){
          $("#fnivel2").addClass("hide");
          $("#fnivel2").removeAttr('pk');
      }
    })
    });
    &lt;/script&gt;
  
  &lt;select id="fnivel"&gt;
    &lt;option value="1"&gt;one&lt;/option&gt;
    &lt;option value="2"&gt;two&lt;/option&gt;
    &lt;option value="3"&gt;three&lt;/option&gt;
  &lt;/select&gt;
  &lt;input id="fnivel2" class="hide" /&gt;
  </code></pre>