How to use ID selector after jQuery html ()

It is amazing to try to understand why a function is alert("hello")not created after clicking more than once ... Is there any way to execute this function?

Please note that after the update does not work, using the html()"click" button.

Any idea? See: http://jsbin.com/atuqu3

JavaScript:

  $(document).ready(function (){
      $("#press").click(function() {
          $("#relation-states").html('<select id="state" name="state"> <option value="Texas">Texas</option> </select><button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>');;
          alert("hello");
      });
  });

HTML:

  <div id="relation-states">
  <select id="state" name="state">
  <option value="New York">New York</option>
  </select>
  <button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>
  </div>
+3
source share
2 answers

div id--, ( DOM ). , , live:

  $("#press").live("click", function() {
      $("#relation-states").html('<select id="state" name="state"> <option value="Texas">Texas</option> </select><button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>');;
      alert("hello");
  });

delegate:

$("#relation-states").delegate("#press", "click", function() {
          $("#relation-states").html('<select id="state" name="state"> <option value="Texas">Texas</option> </select><button id="press" type="button" title="" aria-haspopup="true" style="width: 175px;"><span>Select an item</span></button>');;
          alert("hello");
}
+5

.html() . , , , . , , :

$('#state').html('<option value="Texas">Texas</option>');
+2

Source: https://habr.com/ru/post/1782398/


All Articles