Auto Select the first value of a Select Option

Asked

Viewed 86 times

0

Hello I am using an extension in Chrome called "tampermonkey" and need to create a function that auto select the first value of a Select Option

<select tabindex="-1" class="select-large"><option value="-">[Escolha...]</option> <option value="[object Object]"> NOME </option></select>

In case you will always select the first name!

2 answers

0

Good afternoon, I don’t know if I understand very well but this option tag has the parameter Selected that you can mark for whom you want selected.

ex:

<select id="meu-select" tabindex="-1" class="select-large">
   <option value="-" selected>[Escolha...]</option>
   <option value="[object Object]"> NOME </option>
</select>

View documentation:

https://www.w3schools.com/tags/att_option_selected.asp

if you want to do this in javascript:

// se o value do select ficar igual o value de algum dos option ele será o selecionado
document.getElementById('meu-select').value = '-';

or if you can’t give a field ID you can use for the class it already has:

// lembrando que pela classe podem vir multiplos objetos
document.getElementsByClassName('select-large')[0].value = '-';
  • I’m wanting to use the script on a game page, so I’m using the tampermonkey extension.. Get this:?

  • try using this second code, right in the browser console on the page in question, in cases like this it is interesting you include in the question the link to be able to play the test.

  • You have not selected anything with this code ... I am creating a script for the game the crims

  • goes to the page in question and opens the browser console, with this javascript code the first select with the class 'select-large' will have selected the first record: Document.getElementsByClassName('select-large')[0]. selectedIndex=0; you can do a for with this javascript to set all page selects with this class

0

Test with the following code, remembering that it is necessary to give a name to options or id.

Document.getElementsByName('optionName')[0]. Selected = true

Browser other questions tagged

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