HTML Selected Attribute


The Selected is an attribute of the <option> element, which is used to define the default selection in a dropdown-list.

When any option uses this attribute, then that option should be pre-selected and displayed first, when the web page loads.

Syntax

<option selected>  

Example: The following example uses the Selected attribute in the <option> tag with the Jaguar value.

<!DOCTYPE html>  
<html>  
<head>  
<title>   
Example of selected attribute  
</title>  
</head>  
<body>  
<h4> Select Your favourite Car from the following given options: </h4>  
<br>  
<select name="cars">   
  <option value="Mercedes"> Mercedes </option>  
   <option value="Lamborghini"> Lamborghini </option>  
<!-- In the following option tag we specify the selected attribute for the Jaguar value, which is display as selected on the web page by default. -->  
  <option value="Jaguar" selected> Jaguar </option>  
  <option value="BMW" > BMW </option>  
  <option value="Audi"> Audi </option>   
</select>  
</body>  
</html>  

Output:

HTML Selected Attribute

Example 2: The following example uses the Selected attribute in the <option> tag with the Pomegranate value.

<!DOCTYPE html>  
<html>  
<head>  
<title>   
Second Example of selected attribute  
</title>  
</head>  
<body>  
<h4> Select Your favourite fruit from the following list of fruits: </h4>  
<br>  
<select name="fruits">   
  <option value="Apple"> Apple </option>  
   <option value="Banana"> Banana </option>  
  <option value="Orange"> Orange </option>  
<!-- In the following option tag we specify the selected attribute for the Pomegranate value, which is display as selected on the web page by default.-->  
  <option value="Pomegranate" selected> Pomegranate </option>  
  <option value="Grapes"> Grapes </option>   
   <option value="Mango"> Mango </option>   
</select>  
</body>  
</html>  

Output:

HTML Selected Attribute

Leave a Reply

Your email address will not be published. Required fields are marked *