bdoubleu
bdoubleu

Reputation: 6107

jQuery get the <select> value rather than the selected <option> value

In the above example the console logs a value of 1 which is the currently selected option. How can you log the <select> tag's value of 2?

$(document).ready(function() {
  console.log($('.mySelect').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="mySelect" value="2">
  <option value="1" selected>Choice 1</option>
  <option value="2">Choice 2</option>
  <option value="3">Choice 3</option>
</select>

Upvotes: 1

Views: 58

Answers (2)

Fatihd
Fatihd

Reputation: 645

This would work for you .

console.log( $('.mySelect[value=2]').val() );

Upvotes: -1

Domenik Reitzner
Domenik Reitzner

Reputation: 1613

This is not valid HTML syntax.

Use data attributes like data-someattr for values you want to pass.

Upvotes: 8

Related Questions