As Create a select menu, it is in the first position with the current url

Asked

Viewed 46 times

0

As Create a select menu, it is in the first position with the current url, changing the 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 http://www.test.com/teste4 =

`<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>`

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>Titilo da Pagina</title>
<select >
  <option value="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>

Browser other questions tagged

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