Results 1 to 3 of 3

Thread: Global Variable

  1. #1
    Join Date
    Jun 2006
    San Jose, CA

    Global Variable

    I have an option box with 30 items in 5 groups. To make it easier for the user, I put in a title before each of the groups. This is done in a php function.
    When a user clicks on an item I get the correct name but the wrong selectedIndex because of the titles.
    I would like to adjust the selectedIndex dependent upon which group it is in. This would be done in a javascript function.
    I set up a global variable called $deduct and set the value to which group it is in. I can then adjust the selectedIndex by the value of $deduct.
    I then try to use this in the javascript function but it comes back undefined.
    I have used var $deduct in the javascript function but it doesn't work;
    Is there a way to do this?
    Dick Deeds

  2. #2
    Join Date
    Feb 2006
    use optgroups to separate option groups-
    the optgroup elements are not counted in the options array

    <select name="select_1" size="1">
    <option selected="selected" value="0">Select element</option>
    <option value="1"> Option 1</option>

    <optgroup label="First optgroup">
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <optgroup label="Second optgroup">
    <option value="5">Option 5</option>
    <option value="6">Option 6</option>
    <option value="7">Option 7</option>
    Last edited by mrhoo; 04-14-2010 at 03:11 PM. Reason: de-color

  3. #3
    Join Date
    Jul 2008
    urbana, il
    Quote Originally Posted by mrhoo View Post
    use optgroups to separate option groups-
    the optgroup elements are not counted in the options array
    just to expand on this:
    you can determine the group by looking at the selcted option, and giving the group a memo class:

    <optgroup class="grp1" label="First optgroup">

    //demo select onchange:
    var si=this.selectedIndex, ops=this.options, op=ops[si], mom=op.parentNode.className; alert( "Text: "+ op.text+"\nParent"+mom  )
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center