Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

Why use javascript.void()?

What does "javascript.void()" mean?

If I'm triggering a JS function with a link, I would normally write it like this:

<a href="#" id="doSomething">hit me</a>

And then my code would be something comparable to this:

$('#doSomething').onclick(function() {
    something powerful
});

I'm working on some code that has this:

<a href="javascript:void(0);" class="open-company-details" data-company-id="5e7c9a662bfed251b4281e51" data-route="companies">Loose Cannon Fitness</a>

When I go out to resources such as this:https://www.tutorialspoint.com/What-does-javascript-void-0-mean, I get this definition:

If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “javaScript:void(0)”, returns the undefined primitive value.

It SOUNDS like it's doing something like "#," but maybe a little more?

What does it mean and why would I want to use it as opposed to "#?" I'm thinking the bottom line is to tell the system to basically ignore the normal protocol. "I'm telling my link, Yes,you're normally a link, but, in this instance you're triggering some JS." That's what I'm doing with the "#" character, yes?

So, why javascript.void()?
JavaScript

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bruce Gust

ASKER
Excellent, Michel!

Thank you!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23