setting the value of a select

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cleary1981
    New Member
    • Jun 2008
    • 178

    setting the value of a select

    Hi,

    I want to use javascript to set the value of a select box shown below.

    Code:
     <select id="panel_type">
    <option id="f">FLOOR STANDING</option>
    <option id="w">WALL MOUNTED</option>
    </select>
    for example if this was a text box i would simply write

    Code:
    document.company1.panel_type.value = "FLOOR STANDING";
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    better use the DOM
    Code:
    document.getElementById("f").value = "FLOOR STANDING";
    though, is there any need to do it in javascript (visitors may have javascript turned off)?

    Comment

    • cleary1981
      New Member
      • Jun 2008
      • 178

      #3
      Originally posted by Dormilich
      better use the DOM
      Code:
      document.getElementById("f").value = "FLOOR STANDING";
      though, is there any need to do it in javascript (visitors may have javascript turned off)?
      Im not sure if i explained myself right. I want the value shown in the select box to change to the value I have selected.

      If my select box value shows SELECT OPTION and the drop down tab displays the options FLOOR STANDING and WALL MOUNTED. I want SELECT OPTION to change to FLOOR STANDING.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        If you set the option values:
        Code:
         <select id="panel_type">
        <option id="f" value="FLOOR STANDING">FLOOR STANDING</option>
        <option id="w" value="WALL MOUNTED">WALL MOUNTED</option>
        </select>
        then you can simply set the value:
        Code:
        document.getElementById("panel_type").value = "FLOOR STANDING";
        It would work without setting the option values in some browsers, but IE, for example, will require it.

        Comment

        Working...