[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • 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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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