0

How do I disable a link in jQuery? Previously I was changing the anchor's href attribute but realized this would not be the best way to stop the user being taken to a new page. I added disabled true to the attribute instead but this isn't working. The click still needs to work - I just don't want the link in the anchor to activate.

var acc = document.getElementsByClassName("dropdown-nav");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    this.classList.toggle("active-hit");
    this.find('a').attr('disabled', true);
    //this.find("a").attr("href", "#");
  });
}
<ul>
  <li class="dropdown-nav">
    <a href="/about">About
      <span class="nav-desc">Our company</span>
    </a>
    <div class="hide-border"></div>
    <ul class="second-tier">
      <div class="hide-corner"></div>
      <li><a href="#">Our Mission</a></li>
      <li><a href="#">Our People</a></li>
      <li><a href="#">Work with us</a></li>
      <li><a href="#">High Value Manufacturing</a></li>
    </ul>
  </li>
</ul>

4
  • 2
    links don't have a disabled attribute, question has been asked a bunch of times before. Commented Feb 8, 2018 at 13:57
  • What do you mean by "The click still needs to work - I just don't want the link in the anchor to activate."? Commented Feb 8, 2018 at 13:58
  • 1
    I need to retain the link for desktop view. I'm disabling for an accordion on mobile. Commented Feb 8, 2018 at 13:58
  • So why not just remove the menu on mobile with CSS? Commented Feb 8, 2018 at 14:03

3 Answers 3

3

You can add an event (e) parameter to the click function and the use e.preventDefault(); to stop the link from being followed.

var acc = document.getElementsByClassName("dropdown-nav");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function(e) {
    this.classList.toggle("active-hit");
    e.preventDefault();
  });
}
<ul>
  <li class="dropdown-nav">
    <a href="/about">About
      <span class="nav-desc">Our company</span>
    </a>
    <div class="hide-border"></div>
    <ul class="second-tier">
      <div class="hide-corner"></div>
      <li><a href="#">Our Mission</a></li>
      <li><a href="#">Our People</a></li>
      <li><a href="#">Work with us</a></li>
      <li><a href="#">High Value Manufacturing</a></li>
    </ul>
  </li>
</ul>

Updated jQuery Solution following on from comments:

var $dropdown = $('.dropdown-nav > a');

$dropdown.on('click', function(e){
  $(this).parent().toggleClass('active-hit'); 
  e.preventDefault(); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="dropdown-nav">
    <a href="/about">About
      <span class="nav-desc">Our company</span>
    </a>
    <div class="hide-border"></div>
    <ul class="second-tier">
      <div class="hide-corner"></div>
      <li><a href="#">Our Mission</a></li>
      <li><a href="#">Our People</a></li>
      <li><a href="#">Work with us</a></li>
      <li><a href="#">High Value Manufacturing</a></li>
    </ul>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

6 Comments

Changing to a JQuery function like so doesn't seem to work. $dropdown.click(function(e) { $(this).toggleClass('active-hit'); //$(this).find('a').attr('disabled', true); e.preventDefault(); });
it is the list item .dropdown-nav
Ok trying this out it doesn't allow me to click on the list item (to open the accordion). Are we able to apply the e.preventDefault() to just the anchor which is a child of the list item?
That seems to work - the accordion opens. However the links within (.secon-tier) cannot be followed - they are disabled like the child of the drop-down parent.
Ah yes - so similar to CSS in selecting adjacent element. It's working now.
|
0

I like to use the 'on'

  $("a").on('click', function(event) {
    event.preventDefault();
    alert('default link action prevented');
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="https://www.google.com" target="_blank">Some link</a>

Comments

0

Just add pointer-events: none; css property to the element you want to not be clicked. It makes it just as not visible to your pointer.

Then just use js to toggle this class as you need.

.noClickAllowed {
  pointer-events: none;
}
<ul>
  <li class="dropdown-nav noClickAllowed">
    <a href="/about">About
      <span class="nav-desc">Our company</span>
    </a>
    <div class="hide-border"></div>
    <ul class="second-tier">
      <div class="hide-corner"></div>
      <li><a href="#">Our Mission</a></li>
      <li><a href="#">Our People</a></li>
      <li><a href="#">Work with us</a></li>
      <li><a href="#">High Value Manufacturing</a></li>
    </ul>
  </li>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.