Changing <select> "value".

Discussion in 'JavaScript' started by blueparukia, Apr 4, 2008.

  1. #1
    So I have something like this:

    
    <select name="" id="theid">
    <option value="1">Text</option>
    <option value="2">Something</option>
    </select>
    
    Code (markup):
    Obviously, by default the list is set to "Text", however, when I click a button, I want it to change to "Something".

    How?

    Thanks,

    BP
     
    blueparukia, Apr 4, 2008 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <select name="" id="theid">
    <option value="1">Text</option>
    <option value="2">Something</option>
    </select>
    <input type="button" onclick="document.getElementById('theid').options[1].selected = true;" value="Button" />
    
    Code (markup):
    ?
     
    hogan_h, Apr 4, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Hmmm. Not really.

    Because I have created a PHP/MySQL backend, which generates the ids - and if you delete a row, you will end up with uneven numbers.

    For example, my current select is like:

    
    <select name="" id="theid">
    <option value="1">Text</option>
    <option value="2">Something</option>
    <option value="8">Something Else</option>
    </select>
    
    Code (markup):
    And because of the way it works, if I want "Something Else" to be selected. I have to pass the value "8" through it somewhere. I am not adverse to using id="8" or name="8" if it will help solve my problem.
     
    blueparukia, Apr 18, 2008 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, you need to have a criteria/identifier to be able to select an option item. So you must use either "value" or "text" or index as criteria.

    I'm sorry, but i don't understand what you exactly want, are you talking about selecting an item "server side" or "client side"? Since we are in JS Forum, i suppose you mean client side, so usualy you would just loop through all option items and then select the item which matches specific criteria.
     
    hogan_h, Apr 18, 2008 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    Well this seems to be a bit of a pickle then. And yeah, I mean client side.

    So say I have
    <option value="2" id="Something">Something</option>


    is their anyway to do it like that? The server outputs the value as the ID set in the MySQL database (auto increment), so when you add a few things, then delete them, the next time you add something the value will be much higher.

    At the moment, I use Ajax to retrieve the id from the database, and it has to change the select value to the corresponding word value.
    Thanks a lot,

    BP
     
    blueparukia, Apr 18, 2008 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Got this working, thanks for your help :)
     
    blueparukia, Apr 18, 2008 IP