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

javascript dynamic function problem

chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2.             for (i = 0; i <= 30; i++)
  3.             {
  4.                 var obj = 'date_' + i;
  5.                 document.getElementById(obj).onmouseover = function (event) { alert(i) };
  6.             }
  7.  
date_1, date_2, date_3, ........., date_30 are hyperlink with those ids

i have added the above javascript to show the date on mouse over but no matter which date I go mouse over, it shows 30, and when i set i <= 10 it shows 10
i think it shows the final value of i,

please help me to fix it.

Regards
Apr 11 '09 #1
3 2127
Dormilich
8,658 Expert Mod 8TB
what happened here is (in my opinion) that you incidentally run into a Closure. (this happens when you define a function inside another function or if events are incorporated) the interesting point is, that local variables (here i) are preserved.
Apr 11 '09 #2
chathura86
227 100+
Thanks for the responce

any idea how to fix it?
Apr 12 '09 #3
Dormilich
8,658 Expert Mod 8TB
e.g.
Expand|Select|Wrap|Line Numbers
  1. element.parameter = i;
  2. element.onmouseover = function() { alert(this.parameter); }
some more advanced one
Expand|Select|Wrap|Line Numbers
  1. element.parameter = i;
  2. var show = function() { alert(this.parameter); }
  3. element.addEventListener("mouseover", show, false);
  4. // or use the appropriate cross-browser solution
Apr 12 '09 #4

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

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
2
by: Jens Körte | last post by:
Hi! I've got a typical newbie-problem, I guess. I got some selectboxes on my site. these boxes get their data from a databse. everything works fine, but when a user selects a value from one of...
8
by: Billy Boone | last post by:
I want to put some dynamic text into some href's on my form: <a href="<url>" onMouseOver="window.status='<dynamic text>'; return true" onMouseOut="window.status=''; return true> The dynamic...
1
by: Jorge Ponte | last post by:
hi I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In that WUC I want have a textbox and a button. I want to click on the button and open a popup (I use javascript for...
3
by: Joe Cox | last post by:
I am having a problem with style properties for dynamic images in Mac OS X Safari. By dymanic images, I mean images allocated with the javascript 'new Image()' call. With static images (created...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
8
by: Frank | last post by:
Hi, I am working with VS.NET 2005 Ultimately, I wish to call a JavaScript function from a .js file
1
by: JohnnieTech | last post by:
I am using some javascript/ajax to load content into a main div. The problem I am running into is that it will work in IE but not in FF. In FF I don't get any sort of load at all. I have a 1...
3
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.