[BNM] Javascript: Populating a Hidden Element with a Value onChange of Select Element RESOLVED

Dan Eastwell danieleastwell at onetel.com
Wed Jun 1 13:18:22 BST 2005


This is how I got the code to work using Andy's outline below.

selectedOption =  theForm.selectCategory.selectedIndex;
      //alert("Selected option is" +  selectedOption);
      optionObject = theForm.selectCategory.options[selectedOption];
      //alert("Selected option object is" +  optionObject);
      itsParent = optionObject.parentNode.tagName;
      if(itsParent == 'OPTGROUP'){
          //alert("options parent is" +  itsParent);
          parentsLabel = optionObject.parentNode.label;
          theForm.hiddenElement.value = parentsLabel;
          //alert("options parents label is" +  parentsLabel);

This would be part of a function that runs onsubmit. It fills a hidden 
element in the form, passing this value through the post variables. You 
can thus subdivide options in a select element and return their logical 
category to a processing script.

Many thanks again for all your help.


Andy Smith wrote:

> Dan Eastwell wrote:
>> It would certainly be easier, yes!
>> That would work as it stands, with a little string processing of the 
>> POST variables, but the serving of the page depends on the value of 
>> the optgroup being sent as well as a seperate variable. The 
>> processing page is set up to handle several cases of sets of POST 
>> variables, so generally stripping strings leads to possible problems 
>> for other pages that send variables to the landing page... If you 
>> getwhaddimean.
> How about this: if sel is your <select> element, sel.options should be 
> a list of the <option> elements in it, and sel.selectedIndex is the 
> position of the selected option in that array. So 
> sel.options[sel.selectedIndex] gets you the current option as a DOM 
> object.
> Now take its parentNode. If that's an <optgroup> (if parent.tagName == 
> 'OPTGROUP') you can find out if the selected option is a deely or a 
> doowhacky by checking the optgroup's ID or label or something. If it's 
> a <select> it's not in an <optgroup> and... well, it shouldn't be 
> anything else, I don't think.
> Remember this'll only work when JavaScript is turned on, so if it's a 
> normal site (as opposed to a web application that depends on JS 
> anyway) and the next page needs to know whether the selected option 
> was a deely or a doowhacky, you need to be able to tell which is which 
> without the extra JS info. Even if it is OK to depend on JS it would 
> be possible for a malicious user to send the wrong information in the 
> hidden field, so make sure nothing too bad happens if the user selects 
> a deely but the hidden field says it's a doowhacky, or vice versa.

No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.322 / Virus Database: 267.2.0 - Release Date: 27/05/2005

More information about the BNMList mailing list
BNMList is hosted by Screenlists, a Screen-Play.net service