SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get the value of select tag and copy it to another input tag

    I have a form with select tag in it named "cost". I would like to write a function updateText() that would do the following

    1. get the value of selected option withing that select tag
    2. do some math with like add some other number
    3. copy the result od 2. into input tag named "sum"


    HTML Code:
     <script type="text/javascript">
    function updateText() {
    }
    </script>
    
    <select name="cost" id="cost" onchange="updateText()">
    <option>100</option>
    <option>200</option>
    <option>300</option>
    </select>   
    <input type="text" name="sum" id="sum" />
    Please help me with some tips about what to write in updateText().

    Thanks in advance.

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <script type="text/javascript">
    function 
    updateText(n) {
        var 
    seldocument.getElementById('cost');
        var 
    inputSumdocument.getElementById('sum');
        var 
    selValue sel.options[sel.selectedIndex].value;
        
    inputSum.value= +selValue n;
    }
    </script>
    <select name="cost" id="cost" onchange="updateText(3)">
    <option>100</option>
    <option>200</option>
    <option>300</option>
    </select>   
    <input type="text" name="sum" id="sum" /> 
    Bye.


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
  •