Tom Tom - 2 years ago 114
Javascript Question

How to do "If Clicked Else .."

I am trying to use jQuery to do something like

if(jQuery('#id').click) {
} else {
//run function2

But I'm unsure how to do this using jQuery ? Any help would be greatly appreciated.

Edit: I'm trying to run a function if the #id has been clicked and specifically it is has not been clicked then run the function 2 ? i.e. I need to check firstly that the #id has been clicked before running

Answer Source

You should avoid using global vars, and prefer using .data()

So, you'd do:

  $(this).data('clicked', true);

Then, to check if it was clicked and perform an action:

if(jQuery('#id').data('clicked')) {
    //clicked element, do-some-stuff
} else {
    //run function2

Hope this helps. Cheers

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download