SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript selectbox

    I am trying to figure this out now for a bit and its driving me nuts.

    I have values in a database that are for a select box they are not in a certain order so .listindex is out of the order. I know the value of the select box and send that to my function. how can i change the selected on without listindex.

    like i know the right value is abc

    <option SELECTED value="abc">abc</select>
    <option value="def">def</select>

    see what i want to do is be able to change the selected from one to the other with a function without using listindex as i know how to do that already. and i can't use a if statement on values like abc as they could change if the database values change.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    I'm still puzzled about how you intend to put this together, but here's how you use javascript to work with the select attribute of an element.

    Code Javascript:
    // set the selected attribute
    optionElement.setAttribute('selected', 'selected');
    // get the selected attribute from the element
    value = optionElement.getAttribute('selected');
    // remove the selected attribute
    optionElement.removeAttribute('selected');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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