简体   繁体   中英

Show text when a dropdown option is selected

I am trying to figure out a way to display a some text based on the drop down item selected by the user, in the "result" div below. I know how to do this with a normal input field but I am having trouble understanding how to pass in "option values" into the javascript function. This is what I tried so far...

In the code below, I am simply trying to successfully pass whatever drop down item value is selected into the javascript function and print out the name of that value in the "result" div... once I am able to do that, I will implement the 'tip' feature described above.

My Markup:

<select onChange="dropdownTip(this.value)" name="search_type" style="margin-right:10px; margin-top:2px;">
    <option selected="selected" value="fruit_search">fruits</option>    
    <option value="veggies_search">veggies</option>
    <option value="animals_search">animals</option>
    <option value="all_search">all</option>
</select>

<div id="result"></div>

My JavaScript:

<script type="text/javascript">
    function dropdownTip(value){
        document.getElementByID("result").innerHTML = value;
    }
</script>

Is this what you wanted? check the fiddle below

http://jsfiddle.net/b6ydm/

Try this:

<select onChange="dropdownTip()" id="select" name="search_type" style="margin-right:10px; margin-top:2px;">
    <option selected="selected" value="fruit_search">fruits</option>    
    <option value="veggies_search">veggies</option>
    <option value="animals_search">animals</option>
    <option value="all_search">all</option>
</select>

<div id="result"></div>


<script type="text/javascript">
    function dropdownTip(){
        var value = document.getElementById('select').value;
        document.getElementByID("result").innerHTML = value;
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM