Benjamin Orchard

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromBenjamin Orchard
ToMe
SubjectgetElementByID problems while trying to hide/display multiple divs
Date2 November 2004 17:19
Attachmentthe page with the problem
First, off, I like your tutorial on the DOM, and your site in 
general--its one of the most useful descriptions of the DOM that I've 
encountered. 

I am not javascript coder usually, but this particular page seems to 
need it.  If you can spare the time to take a look at the attached file 
(and my ensuing description, I would really appreciate it.
I have this page, but it doesn't function exactly as expected (ignore 
looks--I am not going to post my css--too much work!).

In the form 'editMS', the user first selects a media type, which then 
displays the appropriate div (and select box) that allows them to select 
the actual media source (which would be a newspaper or radio station).  
This much works.  The user is then supposed to be able to select the 
actual source, and have the sources current status show up, which they 
could then edit.  This second part does not work, because in the 
javascript function 'getOptionSelected' there is the code 
'document.editMS.media' (and its used several times).  So I have 
identified the problem (easy enough).  What I don't know how to do is 
fix it (not a javascript programmer).  This could all be done in PHP and 
page reloads, but I am trying to cut down on server load by using only 
one load. 

At one point I thought that the document.editMS.media(opt_group) could 
be replaced by document.getElementByID('opt_group'), but I can't quite 
get the syntax/method down.  I think its a function of spending far too 
much time playing with databases and PHP, but maybe not. 

If you  can help me fix this, I would deeply appreciate it.

Thanks
Ben Orchard
FromMe
ToBenjamin Orchard
SubjectRe: getElementByID problems while trying to hide/display multiple divs
Date3 November 2004 19:38
Attachmenta fixed version of the page
Ben,

hmm, not entirely sure where to start, but I will try to be gentle :)

unrelated, but ...

<div id="papers" style="display: none; position: relative;">
<label for="papers">Paper</label>
<select name="papers" ...>

label tags target IDs, not names, but form inputs need names (they are supposed
to be able to use IDs, but no browser supports that) so what you should do is to
give both a name and id to the input, making them the same in case any browser
corrects its behaviour. As a separate point, you must not give any two elements
the same id/name:

<div id="papers" style="display: none; position: relative;">
<label for="papersbox">Paper</label>
<select name="papersbox" id="papersbox" ...>

<div id="1"
to quote from the HTML spec:
ID and NAME tokens must begin with a letter (A-Za-z)
This is the main source of your problem. Also, you must hide any of these that
are not in use.

<input type="text" value="1" name="status" />
No two inputs should share the same name. The browser will still submit them
even if they are hidden.

Don't forget that if they have not selected one yet, it should show the correct
entries for the pre-selected option.

I have made an edited version as a demonstration. see attached.

hope this helps you work out what you need


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromBenjamin Orchard
ToMe
SubjectRe: getElementByID problems while trying to hide/display multiple divs
Date4 November 2004 13:15
Mark,
thanks for the advice--I had actually forgotton about the id/name token 
rules, and am actually pulling from a database.  I'll have to rethink my 
strategy just a bit, but that's another story.

I'll let you know how it turns out once I have gotten a littlle further 
on this.

Ben
FromBenjamin Orchard
ToMe
SubjectRe: getElementByID problems while trying to hide/display multiple divs
Date4 November 2004 17:01
Mark,
I have it working now, and thank you for your help.  Now, if you don't 
mind, there is one minor question.

in this snippet,
function getOptionSelected(opt_group){
       return 
document.editMS[opt_group].options[document.editMS[opt_group].selectedIndex].value;
     }

editMS is the name of the form.  Is there a way to make this so that I 
can just pass the name of the form as a variable to the code, and have 
it use that?  Or rather, I am sure that there is a way, but I don't know 
what it is, can you help me out on this (which seems simple enough, but 
it is important that I know).

I am going to guess here and go with this:
function getOptionSelected(form_name, opt_group){
       return 
document[form_name][opt_group].options[document[form_name][opt_group].selectedIndex].value;
     }

Like I said, I'm pretty lost when it comes to javascript.

Ben

Thanks again, BTW--you've helped me a lot!
FromMe
ToBenjamin Orchard
SubjectRe: getElementByID problems while trying to hide/display multiple divs
Date5 November 2004 8:12
Ben,

don't sell yourself short. that was perfect. and I see you have got the
idea that arrays and objects are interchangeable. very good.

Yes, what you have put should work.

Tarquin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.