Skip to content

Instantly share code, notes, and snippets.

@ShigeoTejima
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShigeoTejima/b1e5107feff8b330f10c to your computer and use it in GitHub Desktop.
Save ShigeoTejima/b1e5107feff8b330f10c to your computer and use it in GitHub Desktop.
jQuery example for append and remove element
<!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>
$(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