Results 1 to 6 of 6

Thread: Selected option in JavaScript

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Selected option in JavaScript

    How do I make an option selected using JavaScript function?
    Code:
    document.navform.serie.options.length = 4;
    		 document.navform.serie.options[0].text = "Serie1";
    		 document.navform.serie.options[0].value = "s1";
    		 document.navform.serie.options[1].text = "Serie2";
    		 document.navform.serie.options[1].value = "s2";
    		 document.navform.serie.options[2].text = "Serie3";
    		 document.navform.serie.options[2].value = "s3";
    		 document.navform.serie.options[3].text = "Serie4";
    		 document.navform.serie.options[3].value = "s4";
    I want Serie1 to be selected

  2. #2
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527
    you mean like:

    Code:
    document.navform.serie.options[0].checked = true;
    Blade

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    No it is not a checkbox, so the code must be changed to
    Code:
    document.navform.serie.options[0].selected = true;
    Thanks anyway

  4. #4
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527
    ummm, no.

    it is checked not selected.

    Test out the following HTML:
    Code:
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <FORM Name="Test">
    <INPUT TYPE="RADIO" NAME="Radio1" VALUE=R1>OPtion 1
    <INPUT TYPE="RADIO" NAME="Radio1" VALUE=R2>OPtion 2
    <INPUT TYPE="RADIO" NAME="Radio1" VALUE=R3>OPtion 3
    </FORM>
    <SCRIPT LANGUAGE=JAVASCRIPT>
    document.Test.Radio1[0].checked= true;
    </SCRIPT>
    </BODY>
    </HTML>
    If you replace checked with selected, then it doesn't work.

    Blade

  5. #5
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527
    the other way, of course it do:

    Code:
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <FORM Name="Test">
    <INPUT TYPE="RADIO" NAME="Radio1" VALUE=R1 CHECKED>OPtion 1
    <INPUT TYPE="RADIO" NAME="Radio1" VALUE=R2>OPtion 2
    <INPUT TYPE="RADIO" NAME="Radio1" VALUE=R3>OPtion 3
    </FORM>
    </BODY>
    </HTML>
    Blade

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    That's because you are using the RADIO. I am using a SELECT menu, and then it is SELECTED

Posting Permissions

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



Click Here to Expand Forum to Full Width