user3518127
user3518127

Reputation: 1

trigger click event of the span tag using Javascript

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)

enter image description here

but , it doesn't work. How could I trigger the click event using pure Java script?

Thank you.

Upvotes: 0

Views: 2436

Answers (3)

Oscar Zhang
Oscar Zhang

Reputation: 380

  1. 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.

  2. "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

user4308987
user4308987

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

Spectric
Spectric

Reputation: 31987

Use jQuery.click:

$("span.link").click()

Upvotes: 0

Related Questions