简体   繁体   中英

How to set innerhtml with selected dropdown option text using jquery or javascript?

I want to change the inner text of a anchor tag with the selected drop down option text

Here is my code of dropdown list whatever option is selected in this drop down list i want to update that option's text in another anchor tag

<span style="white-space: nowrap;" class="" id="shopperlanguage_fs">
<select class="input" id="shopperlanguage" name="shopperlanguage">
<option value="ja_JP" class="japImg">japanees</option>
<option selected="" value="en" class="engImg">English (International)</option>/* whatever option is selected like this i want to get this text  */
</select>
</span>

I want to update anchor tag class="dropdown-open" innerhtml with above mentioned dropdown
list selected text

<div class="top-lang clearfix">
<div class="cat-select">
<a href="#" data-dropdown="#dropdown-2" class="dropdown-open"><img src="/site/images 
/lang_05.jpg"> English</a>    /* this is the anchor tag which innerhtml i want to change with selected dropdown option text */
</div>
</div>

Suppose if in dropdown list selected option having text japanees then i want to change the anchor tag innerhtml text english to japanees.

this is my code which i tried but its not working

<script  type="text/javascript">
var e = document.getElementById("shopperlanguage"); 
var strUsertext = e.options[e.selectedIndex].text;
var langValue =$(".dropdown-open").text()
langValue.innerHTML= strUsertext;
</script>

I added <span class="text"></span> to your markup so it would be easier to select the target area:

 $('#shopperlanguage').on('change', function() { $('.cat-select a span.text').text( $('option:selected',this).text() ); }) .change(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <span style="white-space: nowrap;" class="" id="shopperlanguage_fs"> <select class="input" id="shopperlanguage" name="shopperlanguage"> <option value="ja_JP" class="japImg">japanees</option> <option selected="" value="en" class="engImg">English (International)</option> </select> </span> <div class="top-lang clearfix"> <div class="cat-select"> <a href="#" data-dropdown="#dropdown-2" class="dropdown-open"><img src="/site/images /lang_05.jpg"> <span class="text">English</span></a> </div> </div> 

Just try this.. :)

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js">     </script>
<span style="white-space: nowrap;" class="" id="shopperlanguage_fs">
<select class="input" id="shopperlanguage" name="shopperlanguage">
<option value="ja_JP" class="japImg">japanees</option>
<option selected="" value="en" class="engImg">English (International)</option>/* whatever   option is selected like this i want to get this text  */
</select>
</span>
<div class="top-lang clearfix">
<div class="cat-select">
<a href="#" data-dropdown="#dropdown-2" class="dropdown-open">
<img src="/site/images/lang_05.jpg"> English</a>    /* this is the anchor tag which innerhtml i want to change with   selected dropdown option text */
</div>
</div>
<script  type="text/javascript">
$('#shopperlanguage').change(function(){
$('.dropdown-open').text($(this).find('option:selected').text());
});
</script>

try in javaScript

var e = document.getElementById("shopperlanguage");
var strUsertext = e.options[e.selectedIndex].text;
document.getElementsByClassName("dropdown-open")[0].childNodes[1].nodeValue = strUsertext;

DEMO

use On change Jquery with javascript

$('#shopperlanguage').on('change', function () {
    $(".dropdown-open")[0].childNodes[1].nodeValue = this.options[this.selectedIndex].text;
}).change();

DEMO

<select name="abc" id="abc" onchange="chng()">
<option value=""></option>
<option value="English">English</option>
<option value="Japanese">Japanese</option>
</select>


<a href="#" id="atag">English</a>

<script>
function chng()
{
   var s= document.getElementById('abc').value;
   document.getElementById('atag').innerHTML = s;
}
</script>

This is working Demo. Please try it.

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