SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    assign value to option by id

    Hi to all

    Can we assign the value to option by id

    For example we have select

    <select name='test' id='test'>

    <option id='FirstValue' value="1">1</option>
    <option id='SecondValue' value="2">2</option>
    <option id='ThirdValue' value="3">3</option>

    </select>

    Now through javascript can i assign different value to option by id

    document.getElementById('FirstValue').value=10;
    document.getElementById('SecondValue').value=20;
    document.getElementById('ThirdValue').value=30;

    Thanks in advance

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Yes you can
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But when i see the page source i dont see the value although when i assing value and after that i alert that value by geting that value from the opion it give me correct value which i assign

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page source shows the source served by the server. JavaScript modifications to the DOM tree doesn't reflect in the page source. You can use Firebug or Opera's Developer Console to view the live DOM tree.

    Try assigning the values as strings, since the value attribute in HTML is a string.
    Code:
    document.getElementById('FirstValue').value="10";
    document.getElementById('SecondValue').value="20";
    document.getElementById('ThirdValue').value="30";
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for reply

    Its working ...


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
  •