Please suggest me how can I hide href links in Javascript
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello @pwillson
You can achieve this in several ways.
<a href="javascript:void(0)" onclick="location.href='https://actualwizard.com'">Mousing over this URL will display "void(0)" in the browser's status bar.</a>
querySelector <a href="https://example.com" id="myLink">Click me</a>
<script>
// Select the link using querySelector
var link = document.querySelector('#myLink');
// Hide the link visually
link.style.display = 'none';
</script>
Hope that this helps!
You can use CSS to make the link not visible on the page, but it will still be present in the HTML and accessible to screen readers, which might be beneficial for accessibility reasons.
// Hide the link using CSS visibility
document.getElementById('myLink').style.visibility = 'hidden';
This approach completely removes the element from the document flow, as if it doesn’t exist on the page, which can affect your layout.
// Hide the link using CSS display
document.getElementById('myLink').style.display = 'none';
If you want to keep the link visible but non-functional, you can remove the href attribute. The link will remain visible but will not lead anywhere when clicked.
// Remove the href attribute from the link document.getElementById('myLink').removeAttribute('href');
You might want to replace the href with a JavaScript function or simply disable it by pointing it to javascript:void(0); which does nothing when clicked.
// Change the href to a non-functional JavaScript document.getElementById('myLink').href = 'javascript:void(0);';
You can also add an event handler that prevents the default link behavior, effectively disabling the link.
// Prevent the default action of the link document.getElementById('myLink').addEventListener('click', function(event) {
event.preventDefault();
});
Remember to consider the impact on user experience and accessibility when deciding how to handle links in your web applications.
I’m in the same boat as you, just starting out and learning as I go. The tips here have been super helpful. I especially like the idea of using a click event handler to make links non-functional without altering their appearance—really clever!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.