2

I have this code

<!DOCTYPE html>
<html>
<body>

<select multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

</body>
</html> 

I want to select all option at once with JavaScript.

1 Answer 1

14

Here is one way you could do it:

    function selectAll()
    {
    	options = document.getElementsByTagName("option");
    	for ( i=0; i<options.length; i++)
    	{
    		options[i].selected = "true";
    	}
    }
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    
    <select multiple>
    	<option value="volvo">Volvo</option>
    	<option value="saab">Saab</option>
    	<option value="opel">Opel</option>
    	<option value="audi">Audi</option>
    </select>
    
    <button onClick="selectAll();">Select All</button>
    
    </body>
    </html> 

Sign up to request clarification or add additional context in comments.

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.