Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery does not select elements (Wordpress)

I am using Insert Headers and Footers plugin for Wordpress and I am trying to embedd some javascript code will react to the user's actions.

So this is the simple code that I have tried:

$("a#newsLetter").click(function() { 
    alert("element was clicked");  
});

I also tried:

$( "#newsLetter" ).on( "click", function() {
  alert("element was clicked");
});

Both did not work, even though I know that jQuery is loaded and included ( I performed a simple test and of alerting some message on page load)

This is the tag shown in the inspector:

<a href="#" class="elementor-button-link elementor-button elementor-size-xl" role="button" id="newsLetter">

Thanks.

EDIT: This This is how I included jQuery

like image 923
RanAB Avatar asked Aug 03 '26 03:08

RanAB


2 Answers

Use the three parameter form, it applies to elements added after the click handler has been added:

$( document.body ).on( "click", "#newsLetter", function() {
  alert("element was clicked");
});
like image 153
Adder Avatar answered Aug 04 '26 15:08

Adder


If jquery not working then try this

document.getElementById("newsLetter").onclick =function(){
  alert('clecked');
}
<a href="#" class="elementor-button-link elementor-button elementor-size-xl" role="button" id="newsLetter">Link</a>
like image 36
ANIK ISLAM SHOJIB Avatar answered Aug 04 '26 16:08

ANIK ISLAM SHOJIB