1

I will randomly select a number and define the selected field in a select with the sorted number. How I set the field?

2 Answers 2

3

Add the Attribute selected="selected" to the option element that you want to select.

pseudo code

function foo()
{
create list of option elements
generate random number
get a single option element at index of random number
set all options attribute "selected" = null
set the option element[at index of random number] attribute selected = "selected"
}
Sign up to request clarification or add additional context in comments.

Comments

1
var randomNumber = 3; // just for example 
document.getElementById('select_field').selectedIndex = randomNumber;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.