Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check if it is clicked or not

Tags:

jquery

$( 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.

like image 528
gambozygame Avatar asked May 21 '11 12:05

gambozygame


People also ask

How do I know if my element is clicked?

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.

How do you check whether a button is clicked or not Android?

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.


1 Answers

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);     } }); 
like image 196
Felix Kling Avatar answered Sep 25 '22 00:09

Felix Kling