where error in this code javascript

Discussion in 'JavaScript' started by t6werserv, Jan 28, 2010.

  1. #1
    <script type="text/javascript">
    function changeText(){
    var userInput = document.getElementById('sel').selectedIndex.value;
    document.getElementById('view').innerHTML = userInput;
    }
    </script>


    where error?
     
    t6werserv, Jan 28, 2010 IP
  2. bitist

    bitist Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is a working code:

    <form method="post" action="" style="margin:0px">
    <select name="sel" id="sel" onChange="changeText()">
        <option value="1">1 selected</option>
        <option value="2">2 selected</option>
        <option value="3">3 selected</option>
    </select>
    <div id="view"></div>
    </form>
    
    <script type="text/javascript">
    function changeText(){
    	var userInput = document.getElementById('sel').options[document.getElementById('sel').selectedIndex].value;
    	document.getElementById('view').innerHTML = userInput;
    }
    </script>
    Code (markup):
     
    bitist, Jan 28, 2010 IP
  3. t6werserv

    t6werserv Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you, but i want select option only not value!
     
    t6werserv, Jan 28, 2010 IP
  4. bitist

    bitist Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Then instead of:
    var userInput = document.getElementById('sel').options[document.getElementById('sel').selectedIndex].value;
    Code (markup):
    use:
    var userInput = document.getElementById('sel').options[document.getElementById('sel').selectedIndex].text;
    Code (markup):
     
    bitist, Jan 28, 2010 IP
  5. t6werserv

    t6werserv Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    good thank you
     
    t6werserv, Jan 28, 2010 IP
  6. t6werserv

    t6werserv Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6


    <script type="text/javascript">
    function changeText(){
    var chosenoption=document.getElementById("sel").options[document.getElementById('sel').selectedIndex].value;
    if (chosenoption.value="USD")
    {
    document.getElementById("p1").innerHTML="180 USD"
    document.getElementById("p2").innerHTML="200 USD"
    document.getElementById("p3").innerHTML="290 USD"
    }


    if (chosenoption.value="SAR")
    {
    document.getElementById("p1").innerHTML="252 SAR"
    document.getElementById("p2").innerHTML="280 SAR"
    document.getElementById("p3").innerHTML="504 SAR"
    }

    }
    </script>

    where error?
     
    t6werserv, Jan 28, 2010 IP