jquery longclick on <option> element

274 Views Asked by At

i am trying to trigger an event on an option from a dropdown menu. for example:

<select id="select1">
<option id="1">1</option>
<option id="2">1</option>
</select>

i used an example on http://api.jquerymobile.com/taphold/ and appended the event to the option with id=1:

$( "#1" ).bind( "taphold", tapholdHandler );

to test the functionality, the tapholdHandler was just a simple alert. but did not work.

also, i tried to use the

jQuery( "#1" ).on( "tap", function( event ) {alert('works!');} )

but without success. however, i found out that it is possible to append this to the select element, but then it works for all the option elements within the select tag. how can i trigger a longclick/taphold event on a single option element? what i would like to do is to be able to longclick (hold the click) on a desired item and show a dialog which offers to delete the item from the list.

3

There are 3 best solutions below

1
On

your selector is wrong

$('#1') will select an element with id=1

change your code to

$( "#1" ).bind( "taphold", tapholdHandler );

You cannot bind events to <option> elements with any real precision. You can however as stated elsewhere bind to the change event of the select menu in this case

$('#select1').change(function(){
    var selected_option = $(this).find(':selected');
});
1
On

id-selector sector is used like this

$('#1') will select an element with id=1

change your code to

$( "#1" ).bind( "taphold", tapholdHandler );

and

$( "#1" ).on( "tap", function( event ) {alert('works!');} )
2
On

How about binding to 'change'? this will trigger whenever an element is selected or changed.

    $('#select1').change(function(){
         // action here
     });