Not able to handle dynamic <button> elements using :button selector
In the code given below, the buttons part of the original html code are being easily handled by the $(":button") selector. But when I create new buttons using jquery based on value selected from the dropdown menu, the new buttons are not being selected by the :button selector.
-
- <script>
- $(document).ready(function () {
- $(":button").click(function () {
- alert("here");
- })
- });
- </script>
- <select id="thing" name="garden" >
- <option id="u" selected="selected" ></option>
- <option id="1" > Flowers </option>
- <option id="2" > Shrubs </option>
- <option id="3" > Trees </option>
- <option id="4" > Bushes </option>
- <option id="5" > Grass</option>
- <option id="6" > Dirt</option>
- </select>
- <button> + </button>
- <div id="area"></div>
- <button> + </button>
- <script>
- $("#thing").change(function () {
- var str = "";
- var id="";
- var num=1;
- $("#thing option:selected").each(function () {
- str += $(this).text() + " ";
- id = $(this).attr('id');
- $("#"+id).attr('disabled',"disabled");
- });
- if (id != "u") {
- var tx=$("#area").html();
- var button="<button>-</button>";
- $("#area").html(tx+"<div>"+str+" "+button+"</div>");
- };
- }).trigger('change');
- </script>