Home >Web Front-end >JS Tutorial >Why Isn't My JavaScript Function Triggered When I Click a Link?
In an HTML page, a link in the sidebar navigation is intended to update the src attribute of an iFrame containing content. Despite using the correct code, the JavaScript function is not executing when the link is clicked.
Forgetting to invoke the JavaScript function by adding parentheses is the primary cause of the issue. Additionally, there are several recommended practices to follow:
Avoid inline HTML event attributes (onclick, onmouseover, etc.):
Use event listeners (addEventListener()):
Disable hyperlink navigation when using a link for event handling:
<button>
// Add parentheses to invoke the function on link click function getContent() { iFrame.src = "LoremIpsum.html"; console.log("Source updated!"); } // Set up event listener for the button btn.addEventListener("click", getContent);
The above is the detailed content of Why Isn't My JavaScript Function Triggered When I Click a Link?. For more information, please follow other related articles on the PHP Chinese website!