2
<select class=fruits">
<option value="Orange">Orange</option>
<option value="Apple">Apple</option>
<option value="Grapes">Grapes</option>
<option value="Kiwi">Kiwi</option>
</select>

it by default shows orange in the box but I want Grapes in a box. Please help Thanks

tidehunter
  • 31
  • 1
  • 6

6 Answers6

4

For your HTML Dropdown

<select class=fruits">
<option value="Orange">Orange</option>
<option value="Apple">Apple</option>
<option value="Grapes">Grapes</option>
<option value="Kiwi">Kiwi</option>
</select>

Using JS You can use below code to get desired result

document.getElementsByClassName("fruits")[0].selectedIndex = 2 // will select Grapes

Just assign index 0,1..to length of dropdown to get selected value

Rahul Kumar
  • 5,120
  • 5
  • 33
  • 44
S K
  • 442
  • 3
  • 5
3

If you want to use jQuery, then do it like this

$("select.fruits").val("Grapes");

Javascript

var element = document.getElementsByClassName('fruits')[0];
element.value = valueToSelect;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • @tidehunter please do accept answer if it works foryou – Pranay Rana Jan 24 '18 at 07:14
  • There are two pages from the first page if I redirect here it should select grapes automatically. I didn't have the access to the Html page of above code. Please help thank you – tidehunter Jan 24 '18 at 07:42
2

HTML Code:

<select class=fruits">
<option value="Orange">Orange</option>
<option value="Apple">Apple</option>
<option value="Grapes">admin</option>
<option value="Kiwi">Kiwi</option>
</select>

javascript:

$('.fruits option:eq(2)').attr('selected', 'selected');

eq(nth), you can pass index which option you want to select by default. it's start from 0 to n-1.

1
  1. You can use the selector :nth-child() index starts at 1
  2. You can use method .eq() index starts at 0

$(".fruits").find('option').eq(0).css('color', 'blue')//index starts at 0 so first option will be blue
$(".fruits").find('option:nth-child(2)').css('color', 'red');//index starts at 1 so second option will be red
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="fruits">
<option value="Orange">Orange</option>
<option value="Apple">Apple</option>
<option value="Grapes">admin</option>
<option value="Kiwi">Kiwi</option>
</select>
guradio
  • 15,524
  • 4
  • 36
  • 57
1

You can use the selectedIndex property:

$('select.fruits').prop('selectedIndex',  2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="fruits">
<option value="Orange">Orange</option>
<option value="Apple">Apple</option>
<option value="Grapes">Grapes</option>
<option value="Kiwi">Kiwi</option>
</select>

The code above will select the third option, since the index starts from 0.

NcXNaV
  • 1,657
  • 4
  • 14
  • 23
0

In 2023, without jQuery, the correct snippet is this:

document.getElementsByClassName("fruits").selectedIndex = 2
Omiod
  • 11,285
  • 11
  • 53
  • 59