Last active
August 29, 2015 14:27
-
-
Save ShigeoTejima/b1e5107feff8b330f10c to your computer and use it in GitHub Desktop.
jQuery example for append and remove element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE> | |
<html lang="ja"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Demo</title> | |
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> | |
</head> | |
<body> | |
<p id="a">Demo</p> | |
<button id="btn_add">Add</button> | |
<div id="content"> | |
</div> | |
<script> | |
$(function() { | |
$("#btn_add").click(function() { | |
var count = $("#content > button").length; | |
var index = count + 1; | |
var demo = $("<button>") | |
.attr("index", index) | |
.html("Demo " + index) | |
.click( | |
function() { | |
$(this).remove(); | |
} | |
); | |
$("#content").append(demo); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
$("#btn_add").click(function() { | |
var count = $("#content > .content").length; | |
var index = count + 1; | |
var add = $("<div>").addClass("content"); | |
var demo = $("<button>") | |
.attr("index", index) | |
.html("Demo " + index) | |
.click( | |
function() { | |
$(this).closest(".content").remove(); | |
} | |
); | |
add.append($("<p>Index" + index + "</p>")); | |
add.append(demo); | |
$("#content").append(add); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment