Reputation: 1
I'm trying to trigger click event on tag inside the div tag.here is my code
$(document).ready(function () {
$("#span").on('click',function(e){
console.log("clicked in link");
});
});
and here is the html structure (this is a PDF-tron Pdf viewer)
but , it doesn't work. How could I trigger the click event using pure Java script?
Thank you.
Upvotes: 0
Views: 2436
Reputation: 380
The issue is that span is a tag name, not an ID, so instead of $("#span")
you should do $("span")
, but be careful, there might be other span elements there as well.
"How could I trigger the click event using pure Java script?" The trick for PDFTron WebViewer is that it reners the document in an iFrame. So, to access the iframe DOM element, you can do this in the WebViewer constructor, for example:
Webviewer( { /// ... }, document.getElementById('viewer') ).then((instance) => { instance.iframeWindow.document.querySelector('put_your_selector_here').addEventListener('click', () => { console.log('clicked'); }); });
Upvotes: 1
Reputation:
@Spectric has half of the answer really
$("span.link").click()
will click on the span but you incorrectly set the click function
$("#span").on('click',function(e){
the # is for ids not for all elements. So it should be changed to
$("span").on('click',function(e){
or better yet to be more specific like @Spectric said
$("span.link").on('click',function(e){
Upvotes: 0