473,320 Members | 1,821 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,320 software developers and data experts.

for loop and dynamic events

Ciary
247 Expert 100+
Hi all,

i've got another problem. it's based on the question i asked before about creating a onclick-property using javascript.

now what i want to do is create multiple divs with an onclick-property using a for-loop and a 2D array.

Expand|Select|Wrap|Line Numbers
  1. for(var m = 0; m < inputdivs.length; m++){
  2.     var arr = inputdivs[m];
  3.     var newd = document.createElement("div");
  4.  
  5.     newd.innerHTML = arr[0];
  6.     newd.style.width = arr[1];
  7.     newd.style.height = arr[2];
  8.     newd.style.top = arr[3];
  9.     newd.style.left = arr[4];
  10.     newd.style.position = "absolute";
  11.     newd.style.fontWeight = arr[5];
  12.     newd.style.fontStyle = arr[6];
  13.     newd.style.color = arr[7];
  14.     newd.style.textDecoration = arr[8];
  15.     newd.style.fontSize = arr[9];
  16.     newd.style.textAlign = arr[10];
  17.     newd.style.verticalAlign = arr[11];
  18.     newd.id = arr[12];
  19.     newd.style.border = "solid 1px #BBBBBB";
  20.     newd.onclick = function(){myFunct(arr[12])};
  21.     newd.onmouseup = function(){myProb(arr[12])};
  22.  
  23.     $("foo").appendChild(newd);                    //this is prototype meaning document.getelementbyid()
  24. }
  25.  
problem is, the onclick always sends the value of the last created div rather then the value of the div i've clicked on. the strange thing is that all other properties are added fine.

does anyone know whats causing this and how to fix it?
Apr 14 '09 #1
3 2224
Dormilich
8,658 Expert Mod 8TB
could be a Closure*. I could tell you more, if I knew what the two functions in the events are doing.

* see this thread
Apr 14 '09 #2
Ciary
247 Expert 100+
@Dormilich
this worked. but i found out if you use element.parameter instead if this.parameter, you encounter the same problem as not i noted above. so the 'this' is very important

anyway, ty Dormilich. i dont know what i would have done without this :)
Apr 14 '09 #3
Dormilich
8,658 Expert Mod 8TB
@Ciary
of course, element is the/one variable preserved by the Closure.

rule of thumb: if the passed value is fixed (like a string or a number) there's no problem with closures. care is to be taken if the closure appears in a loop.

besides that using "this" is much cooler (*g*) and efficient, once you understand it.
Apr 14 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
4
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
1
by: Martin Falta | last post by:
Hi all, I need a help with following problem. I need to create a menu from dynamic loaded assembly on the main form of my application. The menu is created succesfully, but the events of this...
3
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
1
by: hybrid | last post by:
I have problems in understanding the behavior of the events triggered by dynamically created controls over a webform. Could you help me? In a webform, I have a static PlaceHolder PH containing...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
13
by: Chris Thunell | last post by:
I have created several grids dynamically and have added them to different HTML placeholders on a vb.net web form. The grids and controls within them come up and view beautifully when the web page...
1
by: Kenneth Siewers Møller | last post by:
Hi there I have a question about dynamic event binding. I have a class called Fraction in which a number of events are declared. Some of the events are: OnNameChanged OnCostChanged...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.