homepage Welcome to WebmasterWorld Guest from 54.145.252.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Using document.layers to retrieve an Id.
Using JavaScript.
chris_p

10+ Year Member



 
Msg#: 411 posted 12:47 am on May 20, 2003 (gmt 0)

Hi Guys,

I'm not sure if this is the place to post this question as it relates to JavaScript. But I'll give it a go anyway.
I've been trying to use getElementById to retrieve the id of a drop down list box (e.g. select name="items[]" id="itms") This works fine with most browsers but it doesn't work for NN4 (surprise surprise!).
I am looking at document.layers but am unsure as how to retrieve the id this way. Any advice would be appreciated.

Regards,

Chris.

 

outrun

10+ Year Member



 
Msg#: 411 posted 1:09 am on May 20, 2003 (gmt 0)

I can only show you by example for a simple loop from a simnple page loading script. This should give you a general idea, it can probably help you for IE4 as well.

if (document.getElementById) {
document.getElementById('hidepage').style.visibility = 'hidden';
}
else {
if (document.layers) { // Netscape 4
document.hidepage.visibility = 'hidden';
}
else { // IE 4
document.all.hidepage.style.visibility = 'hidden';
}

regards,
Mark

chris_p

10+ Year Member



 
Msg#: 411 posted 1:48 am on May 20, 2003 (gmt 0)

Hi Mark,

I actually need to retrieve the value of the id.
I have a drop down list box with an array for a name. To access this easier I have given it an Id. Using getElementById I can retrieve the value but can't with document.layers.
E.G. Drop Down List Box.
<select name="array_name[]" id="totals" onchange="add_totals()">

I don't know if document.layers can be used for this purpose. If not, any other suggestions? (I have to use array_name[]).

Regards,

Chris.

rainborick

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 411 posted 11:30 am on May 20, 2003 (gmt 0)

This code may help:

if (document.getElementById) { // standards compliant
theElement = document.getElementById("animated");
maxLeft = wWidth - parseInt(theElement.style.width);
maxTop = wHeight - parseInt(theElement.style.height);
} else if ((document.all) && (!document.getElementById)) { // ie4
theElement = document.all.animated;
maxLeft = wWidth - parseInt(theElement.style.width);
maxTop = wHeight - parseInt(theElement.style.height);
} else if (document.layers) { // ns4
wWidth = window.innerWidth;
wHeight = window.innerHeight;
theElement = document.layers["animated"];
maxLeft = wWidth - parseInt(theElement.document.width);
maxTop = wHeight - parseInt(theElement.document.height);
} // endif ns4/document.layers

Note that this will only work in NS v4.x on a DIV with position:absolute; set. Otherwise, you'll need to add some NS v4.x mark-up using the proprietary <layer> tag.

chris_p

10+ Year Member



 
Msg#: 411 posted 5:56 am on May 21, 2003 (gmt 0)

Hi Rainborick,

Thanks for your response but I don't think I've made myself clear. What I need is to retrieve the value of the list. That is what the user selected from the list. As I said above the drop down has an array name and an id. As you know using getElementById(id).value doesn't work on NN4. The question is, how else can I retrieve what the user has selected?
I have also tried "document.formName.elements[elementId].value" but again this won't work on NN4.

Any advice is greatly appreciated.

Cheers,

Chris.

gph

10+ Year Member



 
Msg#: 411 posted 6:38 am on May 21, 2003 (gmt 0)

Just guessing, I don't have NN4 installed.

document.formName.elementId.value

or

document.formName.elementName.value

chris_p

10+ Year Member



 
Msg#: 411 posted 7:16 am on May 21, 2003 (gmt 0)

That is something that I tried first off but it failed to work. I think it maybe something I am doing incorrectly as I just tried it (elementId) on a simple test page (NN4)and it worked o.k.
I'll persevere.
Watch this space:)

Thanks guys,

Chris.

rainborick

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 411 posted 3:12 pm on May 21, 2003 (gmt 0)

Gosh, I'm sorry. I skimmed your message a bit too quickly last time, and now I'm not sure if I understand. If you have a <select> list and you're using onChange to exectute some JavaScript, some universal code will get you the currently selected value much easier in any browser. Pass the form and the <select> list's name to the function pointed to by your onChange attribute and you can use:

<select name="array_name[]" id="totals" onchange="add_totals(this.form,'array_name[]')">

and then change your JavaScript to read:

function add_totals(theForm,selectList) {
// ...(existing JavaScript to where you need value from list)
theValue = getSelectValue(theForm,selectList);
// ...(existing JavaScript)
} // end add_totals

function getSelectValue(form,listName) {
return form[listName].options[form[listName].selectedIndex].value;
}

Hope this is closer to what you need.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved