• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

onClick event not working on appendChild method

Ive written the script below, I dont understand why clicking on the button doesnt call the message function.
<script type="text/javascript">
 
var numbers= 0;
 
function addfield() {
 
var newButton= document.createElement("input");
 
newButton.ID= "button"+numbers;
newButton.value= "button"+numbers;
newButton.onClick= "message()";
newButton.type="button";
 
 
 
document.getElementById('placeholder').appendChild(newButton);
 
var increment= ++numbers;
numbers= increment;
 
}
 
function message() {
 
alert("Hello There!!!");
 
}
 
</script>
 
 
<a href="#" onClick="addfield()" >add</a>
<div id="placeholder">
 
 
 
</div>

Open in new window

0
agulaid
Asked:
agulaid
2 Solutions
 
Ryan ChongCommented:
quick guess, try change:

<a href="#" onClick="addfield()" >add</a>

to:

<a href="#" onClick="addfield();return false;" >add</a>

?
0
 
Lolly-InkCommented:
Use the following on line 11 of your code:
newButton.onclick= message;

Open in new window

0
 
quincydudeCommented:
If you want multiple statements to run in an onclick event, you can try
newButton.onclick= function(){message();dosomething();};

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now