Let's say I have a link in my DOM (but any element would be accepted), with a call to a Javascript function :
<a href="google.com" onclick="myFunction()">
, and a Jquery function to replace the click on that link and retrieve its href
attribute (sorry, I'm working in Jquery, but feel free to answer in vanilla JS) :
function myFunction() {
$(this).click(function() { return false; }); // Cancel normal behavior.
console.log($(this).attr("href"));
}
This actually doesn't work. Usually, we need to point to an element's ID or Class to get its informations ; but if we can't set an ID or Class or anything and just process some links in a page, how to directly retrieve their informations ?
The title of this topic is intentionally general, because the method should be working for any HTML element.