$( element ).click( function() { });
How can I check if the element is clicked or not? I'm doing like that
function element() { $( "#element" ).click( function() { return 0; } ); } if( element() == 0 ) { alert( "yes" ); } else { alert( "no" ); }
But it's not returning anything.
To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked.
If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.
You could use .data()
:
$("#element").click(function(){ $(this).data('clicked', true); });
and then check it with:
if($('#element').data('clicked')) { alert('yes'); }
To get a better answer you need to provide more information.
Update:
Based on your comment, I understand you want something like:
$("#element").click(function(){ var $this = $(this); if($this.data('clicked')) { func(some, other, parameters); } else { $this.data('clicked', true); func(some, parameter); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With