I created a script where an input field and button will be created based on selection of options. Now I want to execute a function upon clicking the button. Below is my code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="abc">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<div id="sd"> </div>
Function
$(document).ready(function () {
i=0;
$("#abc").on('change',function() {
var maindiv= document.createElement('div');
maindiv.setAttribute("id","TextBoxesGroup");
var secdiv=document.createElement('div');
secdiv.setAttribute("id","TextBoxDiv1")
maindiv.appendChild(secdiv);
var ipt=document.createElement("input");
ipt.setAttribute("type","text");
ipt.setAttribute("id","textbox1");
secdiv.appendChild(ipt);
var ad=document.createElement("input");
ad.value= "+";
ad.type="button";
ad.style.width="30px";
ad.setAttribute("id","addButton"+i);
ad.onclick=fu_a(this.id);
secdiv.appendChild(ad);
var division= document.getElementById("sd");
division.appendChild(maindiv);
i++;
});
});
function fu_a(id){
alert(id);
}
Here upon clicking on button function should get executed. But actually it's getting executed while changing option. Here is my fiddle: