Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2012, 06:39 AM
Registered User
 
Join Date: Jul 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Button event handler

I am new at learning javascript but i was making some sort of ajax application and got stuck.It's got to do something with this code.I want the code to work in a way when i click on 'Click to generate a new button',it should generate a new button and on clicking on the newly created button,the function hola() gets executed.But instead right after clicking the first button,the function hola() gets executed along with appearance of the new button.But after then,the new button is just useless,i.e.,it has no onclick handler attached to it.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
function newButton() {
var button = document.createElement("button");
button.innerHTML = "Click Me";
button.onclick = hola();
document.getElementById('x').appendChild(button);
}
function hola() {
alert('clicked me');
}
</script>
</head>
<body>
<div id="x"></div>
<button onclick="newButton()">Click to generate a new button</button>
</body>
</html>
 
Old July 5th, 2012, 06:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I would change the line where you assign the handler to remove the parentheses so that you assign a function, not the results of a function:
Code:
button = hola;
You might also try assigning the handler after you have added the button to the document.

You might also want to rewrite the code to use a cross-browser library. jQuery has cross-browser support for creating elements and assigning handlers.
__________________
Joe
http://joe.fawcett.name/
 
Old July 5th, 2012, 06:51 AM
Registered User
 
Join Date: Jul 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

ok yeah got it!!
But what if the function requires some parameters.
Like i add this new button to a div element with some id and the action i want on clicking is to delete that div element.For that i have a deleteDiv function which require id of element as a parameter.
 
Old July 5th, 2012, 06:57 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Then you can try setAttribute:
Code:
button.setAttribute("onclick", "hola('myText');");
You just need to be careful with quotes and make sure that variables will be available when the function is called.
__________________
Joe
http://joe.fawcett.name/
 
Old July 5th, 2012, 07:02 AM
Registered User
 
Join Date: Jul 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Well i tried that too.But it does not work.Either the function executes before i intend it to or does not operate at all.





Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW TO ADD AN EVENT HANDLER pallone .NET Framework 2.0 10 September 16th, 2008 12:28 PM
Mouse Event Handler daniel.mihalcea C# 2008 aka C# 3.0 1 September 8th, 2008 06:30 AM
Event handler samir_katore Pro VB 6 6 June 8th, 2006 01:22 PM
PreDefined Event Handler mark C# 1 June 7th, 2003 04:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.