njlqay
njlqay

Reputation: 365

How to select option with double click?

How do I make a select option selectable by double click?

E.g. I double click "Option Text #3" and the value of option three shall be written into a hidden input field with id="selectedOption".

enter image description here

Upvotes: 0

Views: 8482

Answers (3)

Babak Asadzadeh
Babak Asadzadeh

Reputation: 1239

i tried the first answer that returns whole option html.

$('select option').dblclick(function() {
  $('#selectedOption').val($(this).html());
});

use this one instead.

Upvotes: 0

Samyukta R.
Samyukta R.

Reputation: 154

Try this code:

$('select option').dblclick(function() {
  $('#selectedOption').val($("#select option:selected").val());
});

Upvotes: 1

Jai
Jai

Reputation: 74738

You can use dblclick this way:

$('select option').dblclick(function() {
  $('#selectedOption').val(this.outerHTML);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select size="4">
  <option>Option # 1</option>
  <option>Option # 2</option>
  <option>Option # 3</option>
  <option>Option # 4</option>
</select>

<input type='text' id='selectedOption' value=''>

Upvotes: 6

Related Questions