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

onclick assignment in loop not behaving as expected.

I have the following onload function:

[HTML]function slidebarPrep(){
if(!document.getElementById) return false;
var slidebar = document.getElementsByTagName("div");
for(var i = 0; i < slidebar.length; i++){
if(slidebar[i].className == "sub_slidebar"){
var parentId = slidebar[i].offsetParent.id;
var parentHeight = slidebar[i].offsetParent.clientHeight;

var subBox = document.getElementById(parentId);
subBox.style.height = "115px";

slidebar[i].onclick = function(){
sub_expand(50, parentId, parentHeight);
}
}
}
}[/HTML]

I have three div's with parent Id's of submitNews, podcastTopic and VideoSubmissions. So when slidebar[i].onclick I would expect it to output like this:

onclick="sub_expand(50, submitNews, 531);"
onclick="sub_expand(50, podcastTopic, 600);"
onclick="sub_expand(50, videoSubmissions, 425);"

However for some reason each onclick is set to:

onclick="sub_expand(50, videoSubmissions, 425);"

The last div in the loop.

However everything else is set correctly prior to the onclick portion, i.e. each div's hight is set to 115px.

Does anybody have any suggestions?
Dec 29 '07 #1
8 2517
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Can you post the corresponding HTML?

offsetParent is not consistent across the browsers - see this link.
Dec 29 '07 #2
The HTML is as follows, it's the same for each segment:
[HTML]<div id="submitNews">
<p class="submissionInfo">Proin sed mi in enim consectetuer ullamcorper. Duis suscipit cursus turpis. Phasellus diam leo, dictum dapibus, semper ac, pharetra condimentum, leo.</p>
<fieldset class="newsForm">
<ul>
<li><label for="contact_name">Your Name:</label>
<input id="contact_name" type="text" value="<?=$contact_name;?>" size="20" maxlength="50" tabindex="1" /></li>
<li><label for="contact_email">Your E-Mail:</label>
<input id="contact_email" type="text" value="<?=$contact_email;?>" size="40" maxlength="100" tabindex="2" /></li>
<li><label for="contact_city">Your Website:</label>
<input id="contact_city" type="text" value="<?=$contact_city;?>" size="50" maxlength="100" tabindex="3" /></li>
<li><label for="contact_type">News Type:</label>
<select id="contact_type" name="contact_type" tabindex="4">
<option value="false">--- Select One ---</option>
<option value="companyGossip">Company Gossip</option>
<option value="event">Event</option>
<option value="productLaunch">Product Launch</option>
<option value="other">Other</option>
</select>
</li>
<li><label for="contact_message">What's New?:</label>
<textarea id="contact_message" rows="8" cols="40" tabindex="5"><?=$contact_message;?></textarea></li>
</ul>
</fieldset>
<fieldset class="submit">
<input class="submit" type="submit" name="contact_submit" value="Submit" tabindex="6" />
</fieldset>
<div><div class="sub_slidebar"></div></div> <!--Wrapper to fix crap IE bug-->
</div>[/HTML]
You'll notice that the sub_slider div it wrapped in another div. This is why i've had to use offsetParent, the additional div is needed to correct an IE processing issue when an absolute element is proceeds a floated element.

OffsetParent seems to be retreived correctly, at least in FF. But in FF the onclick portion still doesn't work.
Dec 29 '07 #3
Logician
210 100+
Expand|Select|Wrap|Line Numbers
  1.              slidebar[i].onclick = function(){
  2.                 sub_expand(50, parentId, parentHeight);
  3.             }            
  4.  
Try this:
Expand|Select|Wrap|Line Numbers
  1. slidebar[i].onclick = new Function("sub_expand(50,'"+parentId+"', '"+parentHeight+"')");
  2.  
Dec 29 '07 #4
gits
5,390 Expert Mod 4TB
the real problem is, that we have to use a closure when assigning functions with a loop since 'i' would always be the last value of the loop ... so we have to build it the following way (besides the eval which is suggested in the previous post)

the following is what you did and i will always be the last value ... so we get the value 3 alerted:

Expand|Select|Wrap|Line Numbers
  1. var obj = {};
  2.  
  3. for (var i = 0; i < 3; i++) {
  4.     obj[i] = function() {
  5.         alert(i);
  6.     };
  7. }
  8.  
  9. obj[0]();
  10.  
to fix that issue we may use a 'explicit' closure here:

Expand|Select|Wrap|Line Numbers
  1. var obj = {};
  2.  
  3. for (var i = 0; i < 3; i++) {
  4.     obj[i] = function(val) {
  5.         return function() { alert(val) };
  6.     }(i);
  7. }
  8.  
  9. obj[0]();
  10.  
that will alert '0' as we want it ...

kind regards
Dec 29 '07 #5
@Logician = You method works perfectly for me, across all browsers I've tested so thanks you.

@gits = When I was reading your example I understood what you were saying especially seen as that's the behaviour my script was performing, but I could seem to translate your example into my script. :-( I new to javaScript so it's likely just me doing something wrong.

Thanks to both of you though. My issue is now resolved. :-)
Dec 30 '07 #6
gits
5,390 Expert Mod 4TB
hi ...

glad to hear that you got it working ... a small hint for your closure-adaption:

Expand|Select|Wrap|Line Numbers
  1. slidebar[i].onclick = function(p_id, p_height) {
  2.     return function() { sub_expand(50, p_id, p_height); };
  3. }(parentId, parentHeight);
that should do the trick. and should work like the eval-like version ...

kind regards
Dec 30 '07 #7
I understand. Thanks.

So what are the benefits of using your method compared to the eval way?
Dec 30 '07 #8
gits
5,390 Expert Mod 4TB
hi ...

typically we never have to use the Function-constructor or the eval function in our javascript code ... but sometimes we cannot avoid it ... for example to eval a json-string that came as response of an ajax request. the main issue with that are performance issues ... have a look here ... and since we don't need to use it it seems to be 'inelegant' to do so :)

kind regards
Dec 30 '07 #9

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

Similar topics

8
by: DamonChong | last post by:
Hi, I am new and had this piece of code which I created but it is not behaving as expected. I'm using g++, can someone help me out? Thank you. //--------file (Object.cc)----------- class Object...
1
by: Dark Magician | last post by:
Comrades: Am trying to build a UI widget. I'm sure part of the problem is proper variable scope or object reference, and part of the problem may be the way I'm calling the function, but, here...
10
by: drawde83 | last post by:
Hi, I'm mocking up an interface in javascript for an HCI assignment. I want to be able to make the default onclick event for the buttons on my page to display an alert since the buttons won't work...
53
by: usenet | last post by:
See <ul> <li><a name="link1" onClick="alert(this.name);return false;" href="#">Link1</a></li> <li><a name="link2" href="javascript:alert(this);">Link2</a></li> <li>Item 3</li> </ul> ...
5
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler...
5
by: Fred.Grieco | last post by:
Hi every body, I have a little pb and I'm turning around : function MyFCTN(var1,var2) { var mytable = document.getElementById("myTBL"); for (var i=myTBL.childNodes.length-1; i>0; i--){...
8
by: SAL | last post by:
Hello, I have a button on a webform that has an OnClick event defined as such: <asp:Button ID="btnSearchByName" runat="server" Text="Search By Name" OnClick="btnSearchByName_Click" Width="152px"...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
17
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different...
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: 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
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: 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
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
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.