Dynamically add return false to all anchors with # (with javascript or jquery)












-1















I have the following menu in HTML:



At this point I have hardcoded the onclick="return false" right in the HTML (see below). I've read that this is bad practice. I would like to know how to add return false (or e.preventdefault) to all anchors that have a href of # with jquery and/or javascript.



<div id="bar" >
<label id="toggle-label" for="toggle-1">Menu</label>
<input type="checkbox" id="toggle-1">
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
<li><a href="/something">Sub 1.2</a></li>
</ul>
</li>

<li><a href="#" onclick="return false;">Item with sub-sub</a>
<ul>
<li><a href="/something">service-1</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="/something">sub sub 1</a></li>
<li><a href="/something">sub sub 2</a></li>
<li><a href="/something">sub sub 3</a></li>
</ul>
</li>
<li><a href="/electrical">Electrical</a></li>
</ul>
</li>

<li><a href="/pricing">Pricing</a></li>
<li><a href="/contact">Contact Us</a></li>

</ul>

</div>









share|improve this question

























  • What issue are you trying to solve with returning false on the link? I know that cancels the action, but you did not explain what issue allowing the link to go through causes.

    – Taplar
    Nov 13 '18 at 17:38











  • When this menu is viewed on small screens, a click on e.g. Item with sub will make the page scroll to the top because of the # in the anchor-tag.

    – Hakkelaar
    Nov 13 '18 at 17:41













  • Ok, then the next question would be what is the purpose of making these elements links if they are going to not do anything?

    – Taplar
    Nov 13 '18 at 17:42











  • Good question. Only the Item with sub and Item with sub-sub are supposed to have a #. The rest are supposed to go to e.g. /contact and such.

    – Hakkelaar
    Nov 13 '18 at 17:58













  • I think you're missing my point. You're putting anchor markup in your page, but are effectively killing them from doing anything. The only potential reason you'd do that would be if you were using the anchor tags to match some sort of default css styling on the page for links. In which case, rather than "fixing" the links, it would most likely be a better approach to not make them links, and use css to style them to however you want them to look.

    – Taplar
    Nov 13 '18 at 18:00
















-1















I have the following menu in HTML:



At this point I have hardcoded the onclick="return false" right in the HTML (see below). I've read that this is bad practice. I would like to know how to add return false (or e.preventdefault) to all anchors that have a href of # with jquery and/or javascript.



<div id="bar" >
<label id="toggle-label" for="toggle-1">Menu</label>
<input type="checkbox" id="toggle-1">
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
<li><a href="/something">Sub 1.2</a></li>
</ul>
</li>

<li><a href="#" onclick="return false;">Item with sub-sub</a>
<ul>
<li><a href="/something">service-1</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="/something">sub sub 1</a></li>
<li><a href="/something">sub sub 2</a></li>
<li><a href="/something">sub sub 3</a></li>
</ul>
</li>
<li><a href="/electrical">Electrical</a></li>
</ul>
</li>

<li><a href="/pricing">Pricing</a></li>
<li><a href="/contact">Contact Us</a></li>

</ul>

</div>









share|improve this question

























  • What issue are you trying to solve with returning false on the link? I know that cancels the action, but you did not explain what issue allowing the link to go through causes.

    – Taplar
    Nov 13 '18 at 17:38











  • When this menu is viewed on small screens, a click on e.g. Item with sub will make the page scroll to the top because of the # in the anchor-tag.

    – Hakkelaar
    Nov 13 '18 at 17:41













  • Ok, then the next question would be what is the purpose of making these elements links if they are going to not do anything?

    – Taplar
    Nov 13 '18 at 17:42











  • Good question. Only the Item with sub and Item with sub-sub are supposed to have a #. The rest are supposed to go to e.g. /contact and such.

    – Hakkelaar
    Nov 13 '18 at 17:58













  • I think you're missing my point. You're putting anchor markup in your page, but are effectively killing them from doing anything. The only potential reason you'd do that would be if you were using the anchor tags to match some sort of default css styling on the page for links. In which case, rather than "fixing" the links, it would most likely be a better approach to not make them links, and use css to style them to however you want them to look.

    – Taplar
    Nov 13 '18 at 18:00














-1












-1








-1








I have the following menu in HTML:



At this point I have hardcoded the onclick="return false" right in the HTML (see below). I've read that this is bad practice. I would like to know how to add return false (or e.preventdefault) to all anchors that have a href of # with jquery and/or javascript.



<div id="bar" >
<label id="toggle-label" for="toggle-1">Menu</label>
<input type="checkbox" id="toggle-1">
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
<li><a href="/something">Sub 1.2</a></li>
</ul>
</li>

<li><a href="#" onclick="return false;">Item with sub-sub</a>
<ul>
<li><a href="/something">service-1</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="/something">sub sub 1</a></li>
<li><a href="/something">sub sub 2</a></li>
<li><a href="/something">sub sub 3</a></li>
</ul>
</li>
<li><a href="/electrical">Electrical</a></li>
</ul>
</li>

<li><a href="/pricing">Pricing</a></li>
<li><a href="/contact">Contact Us</a></li>

</ul>

</div>









share|improve this question
















I have the following menu in HTML:



At this point I have hardcoded the onclick="return false" right in the HTML (see below). I've read that this is bad practice. I would like to know how to add return false (or e.preventdefault) to all anchors that have a href of # with jquery and/or javascript.



<div id="bar" >
<label id="toggle-label" for="toggle-1">Menu</label>
<input type="checkbox" id="toggle-1">
<ul class="menu">
<li><a href="/">Home</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
<li><a href="/something">Sub 1.2</a></li>
</ul>
</li>

<li><a href="#" onclick="return false;">Item with sub-sub</a>
<ul>
<li><a href="/something">service-1</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="/something">sub sub 1</a></li>
<li><a href="/something">sub sub 2</a></li>
<li><a href="/something">sub sub 3</a></li>
</ul>
</li>
<li><a href="/electrical">Electrical</a></li>
</ul>
</li>

<li><a href="/pricing">Pricing</a></li>
<li><a href="/contact">Contact Us</a></li>

</ul>

</div>






javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:22







Hakkelaar

















asked Nov 13 '18 at 17:35









HakkelaarHakkelaar

255




255













  • What issue are you trying to solve with returning false on the link? I know that cancels the action, but you did not explain what issue allowing the link to go through causes.

    – Taplar
    Nov 13 '18 at 17:38











  • When this menu is viewed on small screens, a click on e.g. Item with sub will make the page scroll to the top because of the # in the anchor-tag.

    – Hakkelaar
    Nov 13 '18 at 17:41













  • Ok, then the next question would be what is the purpose of making these elements links if they are going to not do anything?

    – Taplar
    Nov 13 '18 at 17:42











  • Good question. Only the Item with sub and Item with sub-sub are supposed to have a #. The rest are supposed to go to e.g. /contact and such.

    – Hakkelaar
    Nov 13 '18 at 17:58













  • I think you're missing my point. You're putting anchor markup in your page, but are effectively killing them from doing anything. The only potential reason you'd do that would be if you were using the anchor tags to match some sort of default css styling on the page for links. In which case, rather than "fixing" the links, it would most likely be a better approach to not make them links, and use css to style them to however you want them to look.

    – Taplar
    Nov 13 '18 at 18:00



















  • What issue are you trying to solve with returning false on the link? I know that cancels the action, but you did not explain what issue allowing the link to go through causes.

    – Taplar
    Nov 13 '18 at 17:38











  • When this menu is viewed on small screens, a click on e.g. Item with sub will make the page scroll to the top because of the # in the anchor-tag.

    – Hakkelaar
    Nov 13 '18 at 17:41













  • Ok, then the next question would be what is the purpose of making these elements links if they are going to not do anything?

    – Taplar
    Nov 13 '18 at 17:42











  • Good question. Only the Item with sub and Item with sub-sub are supposed to have a #. The rest are supposed to go to e.g. /contact and such.

    – Hakkelaar
    Nov 13 '18 at 17:58













  • I think you're missing my point. You're putting anchor markup in your page, but are effectively killing them from doing anything. The only potential reason you'd do that would be if you were using the anchor tags to match some sort of default css styling on the page for links. In which case, rather than "fixing" the links, it would most likely be a better approach to not make them links, and use css to style them to however you want them to look.

    – Taplar
    Nov 13 '18 at 18:00

















What issue are you trying to solve with returning false on the link? I know that cancels the action, but you did not explain what issue allowing the link to go through causes.

– Taplar
Nov 13 '18 at 17:38





What issue are you trying to solve with returning false on the link? I know that cancels the action, but you did not explain what issue allowing the link to go through causes.

– Taplar
Nov 13 '18 at 17:38













When this menu is viewed on small screens, a click on e.g. Item with sub will make the page scroll to the top because of the # in the anchor-tag.

– Hakkelaar
Nov 13 '18 at 17:41







When this menu is viewed on small screens, a click on e.g. Item with sub will make the page scroll to the top because of the # in the anchor-tag.

– Hakkelaar
Nov 13 '18 at 17:41















Ok, then the next question would be what is the purpose of making these elements links if they are going to not do anything?

– Taplar
Nov 13 '18 at 17:42





Ok, then the next question would be what is the purpose of making these elements links if they are going to not do anything?

– Taplar
Nov 13 '18 at 17:42













Good question. Only the Item with sub and Item with sub-sub are supposed to have a #. The rest are supposed to go to e.g. /contact and such.

– Hakkelaar
Nov 13 '18 at 17:58







Good question. Only the Item with sub and Item with sub-sub are supposed to have a #. The rest are supposed to go to e.g. /contact and such.

– Hakkelaar
Nov 13 '18 at 17:58















I think you're missing my point. You're putting anchor markup in your page, but are effectively killing them from doing anything. The only potential reason you'd do that would be if you were using the anchor tags to match some sort of default css styling on the page for links. In which case, rather than "fixing" the links, it would most likely be a better approach to not make them links, and use css to style them to however you want them to look.

– Taplar
Nov 13 '18 at 18:00





I think you're missing my point. You're putting anchor markup in your page, but are effectively killing them from doing anything. The only potential reason you'd do that would be if you were using the anchor tags to match some sort of default css styling on the page for links. In which case, rather than "fixing" the links, it would most likely be a better approach to not make them links, and use css to style them to however you want them to look.

– Taplar
Nov 13 '18 at 18:00












3 Answers
3






active

oldest

votes


















2














select all anchors with href='#' using jQuery selector, then add a onclick listener to them.






var anchors = $("a[href='#']")
anchors.on("click", (ev) => {
ev.preventDefault();
console.clear()
console.log("anchor clicked");
return false
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bar" >
<label id="toggle-label" for="toggle-1">Menu</label>
<input type="checkbox" id="toggle-1">
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">Item with sub</a>
<ul>
<li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
<li><a href="#">Sub 1.2</a></li>
</ul>
</li>
<li><a href="#">Item with sub-sub</a>
<ul>
<li><a href="#">service-1</a></li>
<li><a href="#">Item with sub</a>
<ul>
<li><a href="#">sub sub 1</a></li>
<li><a href="#">sub sub 2</a></li>
<li><a href="#">sub sub 3</a></li>
</ul>
</li>
<li><a href="#">Electrical</a></li>
</ul>
</li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>








share|improve this answer


























  • This seems to work great. What's the difference between return false and preventDefault() if I may ask?

    – Hakkelaar
    Nov 13 '18 at 17:48








  • 1





    return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

    – Taplar
    Nov 13 '18 at 17:49











  • yes, it's almost the same, use just one of those, I added both just as examples of usage.

    – Calvin Nunes
    Nov 13 '18 at 17:50











  • @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

    – Hakkelaar
    Nov 13 '18 at 18:00








  • 1





    stackoverflow.com/a/25377783/1586174

    – Taplar
    Nov 13 '18 at 18:03



















1














its very simple you can try this



you need to not only try but also understand and try to do different from this.






$('a[href="#"]').click(function (event) {
return false;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bar" >
<label id="toggle-label" for="toggle-1">Menu</label>
<input type="checkbox" id="toggle-1">
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
<li><a href="#">Sub 1.2</a></li>
</ul>
</li>

<li><a href="#" onclick="return false;">Item with sub-sub</a>
<ul>
<li><a href="#">service-1</a></li>
<li><a href="#" onclick="return false;">Item with sub</a>
<ul>
<li><a href="#">sub sub 1</a></li>
<li><a href="#">sub sub 2</a></li>
<li><a href="#">sub sub 3</a></li>
</ul>
</li>
<li><a href="#">Electrical</a></li>
</ul>
</li>

<li><a href="#">Pricing</a></li>
<li><a href="#">Contact Us</a></li>

</ul>

</div>








share|improve this answer


























  • then do this a[href="#"]' its simple dude

    – dev_ramiz_1707
    Nov 13 '18 at 17:47













  • okay please check this and update it.

    – dev_ramiz_1707
    Nov 13 '18 at 17:49



















0














$('li a[href="#"]').on('click', function(event){
event.preventDefault();
});



You can try this.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286627%2fdynamically-add-return-false-to-all-anchors-with-with-javascript-or-jquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    select all anchors with href='#' using jQuery selector, then add a onclick listener to them.






    var anchors = $("a[href='#']")
    anchors.on("click", (ev) => {
    ev.preventDefault();
    console.clear()
    console.log("anchor clicked");
    return false
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>
    <li><a href="#">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>
    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>








    share|improve this answer


























    • This seems to work great. What's the difference between return false and preventDefault() if I may ask?

      – Hakkelaar
      Nov 13 '18 at 17:48








    • 1





      return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

      – Taplar
      Nov 13 '18 at 17:49











    • yes, it's almost the same, use just one of those, I added both just as examples of usage.

      – Calvin Nunes
      Nov 13 '18 at 17:50











    • @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

      – Hakkelaar
      Nov 13 '18 at 18:00








    • 1





      stackoverflow.com/a/25377783/1586174

      – Taplar
      Nov 13 '18 at 18:03
















    2














    select all anchors with href='#' using jQuery selector, then add a onclick listener to them.






    var anchors = $("a[href='#']")
    anchors.on("click", (ev) => {
    ev.preventDefault();
    console.clear()
    console.log("anchor clicked");
    return false
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>
    <li><a href="#">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>
    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>








    share|improve this answer


























    • This seems to work great. What's the difference between return false and preventDefault() if I may ask?

      – Hakkelaar
      Nov 13 '18 at 17:48








    • 1





      return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

      – Taplar
      Nov 13 '18 at 17:49











    • yes, it's almost the same, use just one of those, I added both just as examples of usage.

      – Calvin Nunes
      Nov 13 '18 at 17:50











    • @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

      – Hakkelaar
      Nov 13 '18 at 18:00








    • 1





      stackoverflow.com/a/25377783/1586174

      – Taplar
      Nov 13 '18 at 18:03














    2












    2








    2







    select all anchors with href='#' using jQuery selector, then add a onclick listener to them.






    var anchors = $("a[href='#']")
    anchors.on("click", (ev) => {
    ev.preventDefault();
    console.clear()
    console.log("anchor clicked");
    return false
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>
    <li><a href="#">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>
    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>








    share|improve this answer















    select all anchors with href='#' using jQuery selector, then add a onclick listener to them.






    var anchors = $("a[href='#']")
    anchors.on("click", (ev) => {
    ev.preventDefault();
    console.clear()
    console.log("anchor clicked");
    return false
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>
    <li><a href="#">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>
    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>








    var anchors = $("a[href='#']")
    anchors.on("click", (ev) => {
    ev.preventDefault();
    console.clear()
    console.log("anchor clicked");
    return false
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>
    <li><a href="#">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>
    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>





    var anchors = $("a[href='#']")
    anchors.on("click", (ev) => {
    ev.preventDefault();
    console.clear()
    console.log("anchor clicked");
    return false
    })

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>
    <li><a href="#">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>
    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 17:48

























    answered Nov 13 '18 at 17:41









    Calvin NunesCalvin Nunes

    3,6372933




    3,6372933













    • This seems to work great. What's the difference between return false and preventDefault() if I may ask?

      – Hakkelaar
      Nov 13 '18 at 17:48








    • 1





      return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

      – Taplar
      Nov 13 '18 at 17:49











    • yes, it's almost the same, use just one of those, I added both just as examples of usage.

      – Calvin Nunes
      Nov 13 '18 at 17:50











    • @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

      – Hakkelaar
      Nov 13 '18 at 18:00








    • 1





      stackoverflow.com/a/25377783/1586174

      – Taplar
      Nov 13 '18 at 18:03



















    • This seems to work great. What's the difference between return false and preventDefault() if I may ask?

      – Hakkelaar
      Nov 13 '18 at 17:48








    • 1





      return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

      – Taplar
      Nov 13 '18 at 17:49











    • yes, it's almost the same, use just one of those, I added both just as examples of usage.

      – Calvin Nunes
      Nov 13 '18 at 17:50











    • @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

      – Hakkelaar
      Nov 13 '18 at 18:00








    • 1





      stackoverflow.com/a/25377783/1586174

      – Taplar
      Nov 13 '18 at 18:03

















    This seems to work great. What's the difference between return false and preventDefault() if I may ask?

    – Hakkelaar
    Nov 13 '18 at 17:48







    This seems to work great. What's the difference between return false and preventDefault() if I may ask?

    – Hakkelaar
    Nov 13 '18 at 17:48






    1




    1





    return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

    – Taplar
    Nov 13 '18 at 17:49





    return false does prevent default, and stop propagation. So really the prevent default call is unnecessary

    – Taplar
    Nov 13 '18 at 17:49













    yes, it's almost the same, use just one of those, I added both just as examples of usage.

    – Calvin Nunes
    Nov 13 '18 at 17:50





    yes, it's almost the same, use just one of those, I added both just as examples of usage.

    – Calvin Nunes
    Nov 13 '18 at 17:50













    @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

    – Hakkelaar
    Nov 13 '18 at 18:00







    @Taplar Is return false like the php-functions exit(); and/or die();? So that it stops all further code?

    – Hakkelaar
    Nov 13 '18 at 18:00






    1




    1





    stackoverflow.com/a/25377783/1586174

    – Taplar
    Nov 13 '18 at 18:03





    stackoverflow.com/a/25377783/1586174

    – Taplar
    Nov 13 '18 at 18:03













    1














    its very simple you can try this



    you need to not only try but also understand and try to do different from this.






    $('a[href="#"]').click(function (event) {
    return false;
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>

    <li><a href="#" onclick="return false;">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>

    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>

    </ul>

    </div>








    share|improve this answer


























    • then do this a[href="#"]' its simple dude

      – dev_ramiz_1707
      Nov 13 '18 at 17:47













    • okay please check this and update it.

      – dev_ramiz_1707
      Nov 13 '18 at 17:49
















    1














    its very simple you can try this



    you need to not only try but also understand and try to do different from this.






    $('a[href="#"]').click(function (event) {
    return false;
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>

    <li><a href="#" onclick="return false;">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>

    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>

    </ul>

    </div>








    share|improve this answer


























    • then do this a[href="#"]' its simple dude

      – dev_ramiz_1707
      Nov 13 '18 at 17:47













    • okay please check this and update it.

      – dev_ramiz_1707
      Nov 13 '18 at 17:49














    1












    1








    1







    its very simple you can try this



    you need to not only try but also understand and try to do different from this.






    $('a[href="#"]').click(function (event) {
    return false;
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>

    <li><a href="#" onclick="return false;">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>

    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>

    </ul>

    </div>








    share|improve this answer















    its very simple you can try this



    you need to not only try but also understand and try to do different from this.






    $('a[href="#"]').click(function (event) {
    return false;
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>

    <li><a href="#" onclick="return false;">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>

    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>

    </ul>

    </div>








    $('a[href="#"]').click(function (event) {
    return false;
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>

    <li><a href="#" onclick="return false;">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>

    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>

    </ul>

    </div>





    $('a[href="#"]').click(function (event) {
    return false;
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="bar" >
    <label id="toggle-label" for="toggle-1">Menu</label>
    <input type="checkbox" id="toggle-1">
    <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="https://google.com" target="_blank">Sub 1.1</a></li>
    <li><a href="#">Sub 1.2</a></li>
    </ul>
    </li>

    <li><a href="#" onclick="return false;">Item with sub-sub</a>
    <ul>
    <li><a href="#">service-1</a></li>
    <li><a href="#" onclick="return false;">Item with sub</a>
    <ul>
    <li><a href="#">sub sub 1</a></li>
    <li><a href="#">sub sub 2</a></li>
    <li><a href="#">sub sub 3</a></li>
    </ul>
    </li>
    <li><a href="#">Electrical</a></li>
    </ul>
    </li>

    <li><a href="#">Pricing</a></li>
    <li><a href="#">Contact Us</a></li>

    </ul>

    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 17:49

























    answered Nov 13 '18 at 17:45









    dev_ramiz_1707dev_ramiz_1707

    420213




    420213













    • then do this a[href="#"]' its simple dude

      – dev_ramiz_1707
      Nov 13 '18 at 17:47













    • okay please check this and update it.

      – dev_ramiz_1707
      Nov 13 '18 at 17:49



















    • then do this a[href="#"]' its simple dude

      – dev_ramiz_1707
      Nov 13 '18 at 17:47













    • okay please check this and update it.

      – dev_ramiz_1707
      Nov 13 '18 at 17:49

















    then do this a[href="#"]' its simple dude

    – dev_ramiz_1707
    Nov 13 '18 at 17:47







    then do this a[href="#"]' its simple dude

    – dev_ramiz_1707
    Nov 13 '18 at 17:47















    okay please check this and update it.

    – dev_ramiz_1707
    Nov 13 '18 at 17:49





    okay please check this and update it.

    – dev_ramiz_1707
    Nov 13 '18 at 17:49











    0














    $('li a[href="#"]').on('click', function(event){
    event.preventDefault();
    });



    You can try this.






    share|improve this answer




























      0














      $('li a[href="#"]').on('click', function(event){
      event.preventDefault();
      });



      You can try this.






      share|improve this answer


























        0












        0








        0







        $('li a[href="#"]').on('click', function(event){
        event.preventDefault();
        });



        You can try this.






        share|improve this answer













        $('li a[href="#"]').on('click', function(event){
        event.preventDefault();
        });



        You can try this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 17:43









        zxyzxy

        1316




        1316






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286627%2fdynamically-add-return-false-to-all-anchors-with-with-javascript-or-jquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine