0

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.

Jaka
  • 1,353
  • 2
  • 10
  • 16

3 Answers3

1

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');
});
DGS
  • 6,015
  • 1
  • 21
  • 37
0

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!');} )
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

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

    $('#select1').change(function(){
         // action here
     });
Steve Papa
  • 422
  • 2
  • 9