2

I followed and tried most answers found on related topics and got issue on this: Check if event is triggered by a human but for some reason (I can't figure it out), my code does not work.

The only difference from the said answered question is I use :

$("#try").get(0).click();

instead of

$("#try").click(); (This won't work on me that's why I'm I use the above code)

Problem:

$("#try").get(0).click(); always returns human triggered event [object MouseEvent].

Is there a way to know exactly if this the click event is triggered physically by humans without changing the event I used?

Note: I use $("#try").get(0).click(); event because I am using frame to show and hide table row information (toggle).

Goal: To only open 1 frame at a time and closing the others (closing of the other frame will be triggered by jQuery event).

test code sample:

$("#try").click(function(event) {
    if (event.originalEvent === undefined) {
        alert('not human')
    } else {
        alert(' human');
    }


});

$('#click').click(function(event) {
   // $("#try").click();
    $("#try").get(0).click();
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Boyd
  • 49
  • 1
  • 1
  • 8
  • .get() works to get an element from an array. an id is unique to one element you should not use .get() while selecting an id. – Gokul Kav Jun 20 '13 at 07:34
  • If `$("#try").click();` doesn't work, but for some reason getting the native JS element from the jQuery selector and calling click on that works, shouldn't you figure out what the problem is, as that is a really weird issue, instead of trying to somehow circumvent it, and why exactly do you need to know what clicked the button, wouldn't you know if you programatically clicked the button ? – adeneo Jun 20 '13 at 07:35
  • @adeneo I wanted to know who triggers the click event because I have other function that process it. (human and jquery event are processed separately) – Boyd Jun 20 '13 at 07:44
  • My point exactly, you're the one writing the jQuery code and triggering the event, so you already know it wasn't a user, so you shouldn't really have to check, but in some very rare cases you have to, something tells me this isn't really one of those cases ? – adeneo Jun 20 '13 at 07:48
  • @adeneo I think this is.. Though I know what really triggers the events but in some case (part of certain requirement) that needs different processing. My issue is how to catch it. (its like passing parameters on what triggers the event) – Boyd Jun 20 '13 at 08:25
  • You can actually do that you know, pass parameters when you trigger an event. – adeneo Jun 20 '13 at 09:44

1 Answers1

4

try with trigger() , however i have no idea why aren't you using $("#try").click(); and why it won't work with you.

$("#try").click(function (event) {
   if (event.originalEvent === undefined) {
     alert('not human')
   } else {
      alert(' human');
   }
});

$('#click').click(function (event) {
  // $("#try").click();
  $("#try").trigger('click');
});

fiddle here

bipen
  • 36,319
  • 9
  • 49
  • 62