Function jQuery Menu select browser url

1

How do I get the code window.location.href right with 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><optionvalue="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: link will be 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>
    
asked by anonymous 19.10.2016 / 05:18

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><optionvalue="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 may also look like this:

$(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><optionvalue="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>
    
19.10.2016 / 14:11