stack_alok stack_alok - 1 month ago 4
HTML Question

Trigger an element click on newly opened browser window using Jquery

I have a web page say main.jsp. I am opening a new window using:

var w=window.open(url);
which opens second.jsp page in new browser tab.

I am trying to trigger click of an element, with class elementToClick, present on second.jsp using:

$(w.document).find('.elementToClick').trigger('click');


However, this is not working.

I am able to do DOM Manipulation like:

$(w.document).find('.elementToClick').html('someHtml');


but not able to trigger events on it.

Please help me in this. I have read other links related to it but couldn't find a solution to this.

Answer

jQuery .trigger('click') will only trigger the click handler associate with the element. It will not actually click the element.

You can use .get() to get the underlying DOM element, then use its click() function

$(w.document).find('.elementToClick').get(0).click();
Comments