How do I create a select menu, which is in the first position with the current url

0

How do I create a select menu, which is in the first position with the current url, changing selected of option :

<select >
  <option value="http://www.test.com/teste1" selected>test1</option>
  <option value="http://www.test.com/teste2">Google</option>
  <option value="http://www.test.com/teste3">YouTube</option>
  <option value="http://www.test.com/teste4">test 4</option>
</select>

Example browser url link =

'<select >
  <option value="http://www.test.com/teste4" selected>test 4</option>
  <option value="http://www.test.com/teste1/">test1</option>
  <option value="http://www.test.com/teste2">Google</option>
  <option value="http://www.test.com/teste3">YouTube</option>
  </select>'
    
asked by anonymous 16.10.2016 / 00:17

1 answer

0

You can do with jquery like this:

// adicionar a nova option e em seguida seleciona-la
$('select').prepend($('<option>', {
    value: window.location.href,
    text: document.title
})).val(window.location.href);

// Abaixo testes de funcionamento:

  // mostrar o value e text inicial
  console.log($('select').val() + ' | ' + $('select option:selected').text());

  // quando o select for alterado mostrar seu value e text
  $('select').change(function(){
    console.log($(this).val() + ' | ' + $(':selected',this).text()); 
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><title>TitilodaPagina</title><select><optionvalue="http://www.test.test.1/" selected>test1</option>
  <option value="http://www.test.test.2">Google</option>
  <option value="http://www.test.test.3">YouTube</option>
  <option value="http://www.test.test.4">test 4</option>
</select>
    
16.10.2016 / 12:29