473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

create element and set onclick function

Hi, here is my code:
<html>
<head>
<script type="text/javascript">

function setImage(num)
{
alert(num);
}

function init()
{
for (var i = 0; i < 5; i++){
var button = document.createElement("input");
button.type = "button";
button.value = "Image ";
button.onclick = setImage(button);
document.body.appendChild(button);
}
}
</script>
</head>
<body onload="init();">
</body>
</html>

why it is wrong? How to fix it?

Thanks,

qq

Jul 16 '06 #1
1 3694
qu******@yahoo.com wrote:
Hi, here is my code:
<html>
<head>
<script type="text/javascript">

function setImage(num)
You seem to expect to pass a value to the function, but your code below
doesn't do that. I'll guess that you are trying to pass 'i' from init's
for loop.

{
alert(num);
}

function init()
{
for (var i = 0; i < 5; i++){
var button = document.createElement("input");
button.type = "button";
button.value = "Image ";
button.onclick = setImage(button);
That will assign the result of setImage(button) to the value of the
button's onclick attribute. What you need to do is assign a reference
to the function itself:

button.onclick = setImage;

But you want to pass a value, so the next thing that you might try is:

button.onclick = function(){ setImage(i); }
Which 'works', but it creates a closure back to the init's local i. All
the buttons will alert '5', which is the value of i when the function
finished, not its value when each onclick handler was set.

There are a couple of ways to break the closure, one is to use the
Function object as a constructor, but that is not liked as it is
considered only marginally better than using eval. You can either
create a separate function to add the onclick:

function addOnclick(obj, func, val){
obj.onclick = function(){func(val)};
}

and call it like:

addOnclick(input, setImage, i);
or you can use a function expression:

input.onclick = (function(i){
return function(){ setImage(i);}
})(i);
which is somewhat more convoluted, but probably neater overall.

[...]

--
Rob
Jul 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: js | last post by:
Two questions: 1. I created Javascript objects called oNewObj using {} construct as in the follwoing code segment. When I clicked on the <td> element, I got runtime error "oNewObj is undefined"....
4
by: Michael Hill | last post by:
I had this html: <tr id="action" title="click to do something" onclick="alert('mike');"> <td>a</td> <td>b</td> </tr> and it works when I click on the row, but when I try to add it...
2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
3
by: takarimasu | last post by:
How can i create an input object (text area,select) at runtime ? B.
10
by: Noozer | last post by:
Is it possible to detect where on a page the click occurred when the OnClick event of the BODY tag is fired? Thx
1
by: shadow.demon | last post by:
G'day guys, i've looked around but i can't find how to create an A tag with a onclick to remove a row in a table. Can add rows to table, but i can't get it to create a link in the last coloumn...
3
by: Beamer | last post by:
Hi I am trying to build a roating slide effect in javascript. Basically, I have a list like below <ul id="slideShowCnt"> <li id="slide0"><img .../></li> <li id="slide0"><img .../></li> <li...
14
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.