0

How can I simulate a click on x/y coords on the stats_wrapper?

<div class="stats_wrapper">
    <span class="like_icon"></span>
    <span class="number_of_likes">
        10
    </span>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
sillyous
  • 1
  • 2
  • Do you need the x/y-coords of the click happend or do you just need this element programmatically clicked? – empiric May 13 '16 at 11:17
  • What do you mean by "simulate a click (X/Y coords.)"? Why would you need that? What is your expected result? Isn't this HTML wrapped in a iframe (cross domain)/third party plugin? – A. Wolff May 13 '16 at 11:17
  • Everyone would appreciate if you share the user case – Velimir Tchatchevsky May 13 '16 at 11:31
  • I need to click the : like_icon by using coordinates or click: like_icon only inside the div named: stats_wrapper . I'm not allowed to use if statements , quite a challenge – sillyous May 13 '16 at 11:42

3 Answers3

1

Set the pageX and pageY properties (which are normalized) on the event object and pass it to .trigger(), like this:

var e = new jQuery.Event("click");
e.pageX = 10;
e.pageY = 10;
$("#elem").trigger(e);

Cited form Triggering a JavaScript click() event at specific coordinates

Community
  • 1
  • 1
preator
  • 984
  • 5
  • 6
0

Why would you want to go with X/Y coordinates?

If I were you, I'd use the event dispatcher.

var down = new MouseEvent('mousedown') 
var up = new MouseEvent('mouseup');
var elem = document.getElementsByClassName("stats_wrapper")[0];
elem.dispatchEvent(down);
elem.dispatchEvent(up);

That way, you tell your div that it was clicked and you do not need to worry about coordinates. Obviously, I don't know your use case so it might not be what your looking for.

Edit: This is a pure JS solution. For a JQuery solution, you should use $(.stats_wrapper).trigger('click') as mentionned by Velimir

-1

To simulate a click on .stats_wrapper you can use this (jQuery): $(.stats_wrapper).trigger("click"); You can also click any element inside it instead, I see no function of triggering a click on a specific pixel within a single element

Velimir Tchatchevsky
  • 2,812
  • 1
  • 16
  • 21