0

I have this html data:

<select class="select">
    <option>a</option>
    <option>b</option>
    <option>c</option>
</select>

How do I bind an event only when I click the select element shown in the page, not when the option elements are clicked. I don't want to bind event on the items that are in the dropdown.

Update: I did found this one post. But it does not work for my case.

Rabin Lama Dong
  • 2,422
  • 1
  • 27
  • 33

3 Answers3

1

Try this:

$(function(){
$('select').on('focus',function(){
console.log($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="select">
    <option>a</option>
    <option>b</option>
    <option>c</option>
</select>
lalithkumar
  • 3,480
  • 4
  • 24
  • 40
0

This does the job for now.

$('.select').on('focus', function() {
    //code here
});

But I'm not sure if it is good for long run.

Rabin Lama Dong
  • 2,422
  • 1
  • 27
  • 33
0

try :

$('select').focus(function(){...}); 

or

$('select').click(function(){...}); 
Priyank
  • 3,778
  • 3
  • 29
  • 48
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33