Create Button again and again OnClick

Tabrez
Posted by Tabrez under jQuery category on | Points: 40 | Views : 1896
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.js"></script>
<title>Create Button Again and Again</title>
<style>
.parallel{
border:3px solid red;
display:inline-block;
}
</style>
</head>

<body>
<div id="KeepContent" class="parallel">
<button onclick="dynamichtml();">Create Button</button>
</div>

<script type="text/javascript">
var count = 0;
function dynamichtml() {
var d = $("#KeepContent");

var btn = document.createElement("input");
btn.type = "button";
btn.id = "btn_"+count;
btn.value = "New Button";

d.delegate('#btn_'+count, "click", function () {
dynamichtml();
});

d.append("<br/>");
d.append("<br/>");
d.append(btn);
d.append("<br/>");
count++;
}

</script>
</body>
</html>

Comments or Responses

Posted by: Amatya on: 2/16/2016 Level:Silver | Status: [Member] | Points: 10
What is the use of this code.. where we can use.. Have you used in application?
Posted by: Tabrez on: 2/16/2016 Level:Starter | Status: [Member] | Points: 10
In most of the situations we create some dynamic controls when user want to give extra information about her/himself like A Button for Add Extra Curricular, Add More Work Experience in that situation we create some dynamic elements for add that and bind the Submit button with an event to store the data.
It is just for understanding how to register delegate on previous button or new button that's it.

Login to post response