Link to home
Start Free TrialLog in
Avatar of agulaid
agulaid

asked on

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

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

quick guess, try change:

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

to:

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

?
ASKER CERTIFIED SOLUTION
Avatar of Lolly-Ink
Lolly-Ink
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial