How do I add newObject to my array shapeArr?
Do I need to add a for loop?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
ul{ list-style-type:none;}
div{ width:300px; height:200px; background-color:#0066cc; }
</style>
</head>
<body>
<ul id="placeholder"></ul>
<a href="#" id="btn">Add</a>
<script type="text/javascript">
function add(){
function draw(){
var template = document.createElement("li");
template.innerHTML = "<div></div><a href='#' class='update'>Update</a>";
document.getElementById("placeholder").appendChild(template);
}
var newObject = new draw();
var shapeArr = [];
}
var btn = document.getElementById("btn");
btn.addEventListener("click", add, false);
</script>
</body>
</html>
asked Oct 16, 2012 at 23:46
Nasir
4,90511 gold badges37 silver badges39 bronze badges
-
2Your question is too broad to be answered accurately. Why don't you reduce your code to the parts relevant to the question and further explain what it is you're trying to do?David G– David G2012年10月16日 23:48:36 +00:00Commented Oct 16, 2012 at 23:48
2 Answers 2
Use Array.push().
shapeArr.push(newObject);
answered Oct 16, 2012 at 23:50
Musa
97.9k17 gold badges123 silver badges144 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can use the push() method of the JavaScript Array to add new elements to the end:
var newObject = new draw();
var shapeArr = [];
// add the new Object
shapeArr.push(newObject);
answered Oct 16, 2012 at 23:48
Hunter McMillen
61.8k25 gold badges124 silver badges176 bronze badges
Comments
lang-js