I need to make an automated selection

Asked

Viewed 27 times

0

Hello, I would like help to execute an automatic selection command on this:

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

Because I am doing a project that when entering the page appears the names of several people to select and this is very annoying, because anyone on the list serves.. So I downloaded the tampermonkey and I need a code that clicks and selects a random person from the list.

  • Hi lololololo, welcome to the community! value="[object Object]" come from where? Just like this you won’t be able to use the value of that select. Because you need the tampermonkey? is for the functionality you ask here?

2 answers

0

It worked, thank you very much !!

//Elementos
const select = document.querySelector('select');
const options = document.querySelectorAll('option');

//Cria um número randômico entre 0 e 1
//Multiplica pela quantidade de opções
//Arredonda para o inteóiro mais prximo
let op = Math.round(Math.random() * (options.length - 1));

console.log(op);

//Define o valor do select para o valor de uma das opção da lista
select.value = options[op].value;
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

0

//Elementos
const select = document.querySelector('select');
const options = document.querySelectorAll('option');

//Cria um número randômico entre 0 e 1
//Multiplica pela quantidade de opções
//Arredonda para o inteóiro mais prximo
let op = Math.round(Math.random() * (options.length - 1));

console.log(op);

//Define o valor do select para o valor de uma das opção da lista
select.value = options[op].value;
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

Browser other questions tagged

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