MJ
asked on
JavaScript- Detect when Anchor Tag Element is clicked? (ES5)
How to detect when element is Clicked using JavaScript?
<a data-target="#botwDisclosuresModal" data-toggle="modal" href="#botwDisclosuresModal"> Application Disclosures </a>
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<a data-target="#botwDisclosuresModal" data-toggle="modal" href="#botwDisclosuresModal" onclick="function(){alert('clicked - you can also call a function here');"> Application Disclosures </a>
https://www.w3schools.com/jsref/event_onclick.asp
ASKER
Sorry, Geoff! I should have mentioned that I can't modify the element in any way. I have to use a TMS.
what miss in my solutions?
Then add the event listener as suggested above 😊.  Using JQuery is the easiest by far.
Use $("[href=#botwDisclosuresM odal]").cl ick(functi on(){ //function code here });
Use $("[href=#botwDisclosuresM
You can detect if the tagName is anchor ('A') like this
HTML
JS script
JSFiddle here
HTML
<div><a data-target="#botwDisclosuresModal" data-toggle="modal" href="#botwDisclosuresModal">
Application Disclosures
</a></div><br/>
<div data-target="#botwDisclosuresModal" data-toggle="modal" href="#botwDisclosuresModal">
Application Disclosures Div Element
</div>
JS script
document.addEventListener('click',function(a){
if (a.target.tagName=='A'){
console.log(true)
// You can add your code here
}else{
console.log(false)
}
});
JSFiddle here
ASKER
Thank you all!
you welcome!
Open in new window