jQuery Menu function select url browser

Asked

Viewed 41 times

1

How do I make the code window.location.href with the right value of <option:

<script type="text/javascript">
    jQuery(document).ready(function($){
          $('select').find('option[value=window.location.href]').attr('selected','selected');
        });
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select>
   <option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
<option value="http://www.test.com/page8">United States dollar (USD)</option>
</select>

Example url browser:http://www.test.com/page8 be the selected

<select>
<option value="http://www.test.com/page8" selected="selected" >United States dollar (USD)</option>
<option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
</select>

1 answer

1


See if that’s what you need:

$(function(){
	$('select').find('option').each(function(key){
		if($(this).val() == 'http://www.test.com/page8'){
          $(this).attr('selected', 'selected');  
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<select>
   <option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
<option value="http://www.test.com/page8">United States dollar (USD)</option>
</select>

It can be that way too:

$(function(){
var url = "http://www.test.com/page8";
  $('select').find('option[value="' + url +'"]').attr('selected', 'selected');  
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<select>
   <option value="http://www.test.com/page0">Japanese yen (JPY)</option>
<option value="http://www.test.com/page1">Afghan afghani (AFN)</option>
<option value="http://www.test.com/page2">Albanian lek (ALL)</option>
<option value="http://www.test.com/page3">Algerian dinar (DZD)</option>
<option value="http://www.test.com/page4">Angolan kwanza (AOA)</option>
<option value="http://www.test.com/page5">Argentine peso (ARS)</option>
<option value="http://www.test.com/page6">Armenian dram (AMD)</option>
<option value="http://www.test.com/page7">Aruban florin (AWG)</option>
<option value="http://www.test.com/page8">United States dollar (USD)</option>
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.