I have the following onload function:
[HTML]function slidebarPrep(){
if(!document.ge tElementById) return false;
var slidebar = document.getEle mentsByTagName( "div");
for(var i = 0; i < slidebar.length ; i++){
if(slidebar[i].className == "sub_slidebar") {
var parentId = slidebar[i].offsetParent.i d;
var parentHeight = slidebar[i].offsetParent.c lientHeight;
var subBox = document.getEle mentById(parent Id);
subBox.style.he ight = "115px";
slidebar[i].onclick = function(){
sub_expand(50, parentId, parentHeight);
}
}
}
}[/HTML]
I have three div's with parent Id's of submitNews, podcastTopic and VideoSubmission s. So when slidebar[i].onclick I would expect it to output like this:
onclick="sub_ex pand(50, submitNews, 531);"
onclick="sub_ex pand(50, podcastTopic, 600);"
onclick="sub_ex pand(50, videoSubmission s, 425);"
However for some reason each onclick is set to:
onclick="sub_ex pand(50, videoSubmission s, 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?
8 2525 acoder 16,027
Recognized Expert Moderator MVP
Welcome to TSDN!
Can you post the corresponding HTML?
offsetParent is not consistent across the browsers - see this link.
The HTML is as follows, it's the same for each segment:
[HTML]<div id="submitNews" >
<p class="submissi onInfo">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_na me">Your Name:</label>
<input id="contact_nam e" type="text" value="<?=$cont act_name;?>" size="20" maxlength="50" tabindex="1" /></li>
<li><label for="contact_em ail">Your E-Mail:</label>
<input id="contact_ema il" type="text" value="<?=$cont act_email;?>" size="40" maxlength="100" tabindex="2" /></li>
<li><label for="contact_ci ty">Your Website:</label>
<input id="contact_cit y" type="text" value="<?=$cont act_city;?>" size="50" maxlength="100" tabindex="3" /></li>
<li><label for="contact_ty pe">News Type:</label>
<select id="contact_typ e" name="contact_t ype" tabindex="4">
<option value="false">--- Select One ---</option>
<option value="companyG ossip">Company Gossip</option>
<option value="event">E vent</option>
<option value="productL aunch">Product Launch</option>
<option value="other">O ther</option>
</select>
</li>
<li><label for="contact_me ssage">What's New?:</label>
<textarea id="contact_mes sage" rows="8" cols="40" tabindex="5"><? =$contact_messa ge;?></textarea></li>
</ul>
</fieldset>
<fieldset class="submit">
<input class="submit" type="submit" name="contact_s ubmit" value="Submit" tabindex="6" />
</fieldset>
<div><div class="sub_slid ebar"></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.
- slidebar[i].onclick = function(){
-
sub_expand(50, parentId, parentHeight);
-
}
-
Try this: - slidebar[i].onclick = new Function("sub_expand(50,'"+parentId+"', '"+parentHeight+"')");
-
gits 5,390
Recognized Expert Moderator Expert
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: - var obj = {};
-
-
for (var i = 0; i < 3; i++) {
-
obj[i] = function() {
-
alert(i);
-
};
-
}
-
-
obj[0]();
-
to fix that issue we may use a 'explicit' closure here: - var obj = {};
-
-
for (var i = 0; i < 3; i++) {
-
obj[i] = function(val) {
-
return function() { alert(val) };
-
}(i);
-
}
-
-
obj[0]();
-
that will alert '0' as we want it ...
kind regards
@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. :-)
gits 5,390
Recognized Expert Moderator Expert
hi ...
glad to hear that you got it working ... a small hint for your closure-adaption: - slidebar[i].onclick = function(p_id, p_height) {
-
return function() { sub_expand(50, p_id, p_height); };
-
}(parentId, parentHeight);
that should do the trick. and should work like the eval-like version ...
kind regards
I understand. Thanks.
So what are the benefits of using your method compared to the eval way?
gits 5,390
Recognized Expert Moderator Expert
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 {
public:
Object(const char * c){
id = c;
}
|
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 goes.
Part I. Consider:
A B C D M
E F G H N O
|
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 yet. Is there an easy
way to do this? and it has to be as easy as giving each button their
own onclick attribute otherwise I'll just do that.
|
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>
Clicking on the first list item gives me "link1" because "this" refers
|
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 complains.
class myclass
| |
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--){
myTBL.removeChild(myTBL.childNodes);
}
for(var i=0; i<var1.length; i++){
|
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" EnableViewState="False"
CausesValidation="False" />
the btnSearchByName_Click event fires the second time the button is clicked
but not the...
|
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.
Furthermore, he doesn't even mark the assignments, but rather gives
tips and so forth when going over students' work. To test students'...
|
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
things? Shouldn't they say the same thing?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |