473,380 Members | 1,421 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,380 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 2125
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.