简体   繁体   中英

Javascript - Replacing Text using option

I'm trying to replace the text "JSON String Here" but when I select an item from the options, it disappears. I'm new to programming Javascript, can someone take a look at this? THanks!

<title>Local Testing</title>

 <script>

    function swapJson(){

      var div = document.getElementById("jsonToSwap");
      var dropdown = document.getElementById("dd");
      div.src = dropdown.options[dropdown.selectedIndex].value; 
      document.write(div.src);

    };

 </script>

<div id="jsonToSwap">JSON String Here</div>

<select id="dd" onChange="swapJson()">
    <option value="1" id="1">Headset</option>
    <option value="2" id="2">Taxi</option>
    <option value="3" id="3">Buildings</option>
</select>

change the line that reads

div.src = dropdown.options[dropdown.selectedIndex].value; 

to this

div.innerText = dropdown.options[dropdown.selectedIndex].value;     

so the whole thing will look like this

<script>

    function swapJson(){

      var div = document.getElementById("jsonToSwap");
      var dropdown = document.getElementById("dd");
      div.innerText = dropdown.options[dropdown.selectedIndex].value;
    };

 </script>

Example
http://jsfiddle.net/FxwQW/1/

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