Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on Jun 6, 2024
To trigger a javascript href in LWC, you just need to use innerHTML.
import { LightningElement } from "lwc";
export default class App extends LightningElement {
renderedCallback() {
const content = 'Google';
this.template.querySelector('div')[removed] = content;
}
}
Or, if you mean to say you want to redirect to a website:
[removed].href = 'https://www.google.com/';
Or, if you want to trigger a download:
const a = document.createElement('a');
a.href = 'https://www.google.com';
document.body.appendChild(a); // Firefox apparently requires this
a.click();
document.body.removeChild(a);
Best Answer · By JanBask Salesforce Expert
Ranjana Admin JanBask Expert
Answered on Jun 6, 2024
In Salesforce Lightning Web Components (LWC), you can trigger a href link using JavaScript. This can be done by dynamically creating and dispatching a click event on an anchor () element. Here's a step-by-step guide on how to achieve this:
Step-by-Step Guide
Create the LWC Component:
1. First, create a new Lightning Web Component. For example, let's name it navigateLink.
2. Component HTML Template (navigateLink.html):
In the HTML file, you don't need to have the actual element visible if you are dynamically creating it through JavaScript. However, you can have a button or any other trigger element.
3. Component JavaScript Controller (navigateLink.js):
export default class NavigateLink extends LightningElement { handleNavigate() { // Create a new anchor element const anchor = document.createElement('a'); // Set the href attribute to the desired URL anchor.href = 'https://www.example.com'; // Set the target attribute if you want to open in a new tab anchor.target = '_blank'; // Append the anchor to the document body document.body.appendChild(anchor); // Trigger the click event on the anchor element anchor.click(); // Remove the anchor from the document body document.body.removeChild(anchor); }}CSS (navigateLink.css):
You might not need CSS specifically for this functionality, but if you do, create the CSS file accordingly.
/* Example: Optional styling if needed */Explanation
Usage
1 Button Click: When the button is clicked, the handleNavigate method is called, which handles the creation and triggering of the anchor element's click event.
This approach ensures that you can dynamically navigate to any URL using JavaScript within an LWC component, adhering to the best practices of Salesforce Lightning Web Components.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Prepare for Salesforce Developer Certification: A Practical Guide
A step-by-step guide to Salesforce Developer certification - what to study for PD1, how PD2 fits in, and how to know you're ready…
Salesforce Salesforce Developer Certification Guide: Everything You Need to Know in 2026
Learn about Salesforce Developer Certification, including exam requirements, cost, topics, preparation time, retakes, and career…
Salesforce How to Prepare for Salesforce Admin Certification: A Practical Study Guide
Get a practical study plan for the Salesforce Admin exam - weighting by section, how to use Trailhead effectively, and how to…
Salesforce Salesforce Developer Salary Guide: How much you can earn
Discover the real Salesforce developer salary in 2026 by experience, location, skills & certifications. See what actually boosts…