3

I have the following HTML.

<ul class="static">
    <li class="static dynamic-children">
        <a class="static dynamic-children menu-item" href="test.php" tabindex="0">
            <span class="additional-background">
                <span class="menu-item-text">Link</span>
            </span>
        </a>
        <ul class="dynamic">
        </ul>
     </li>
</ul>

As you can see when I click on "Link" then it takes me to "test.php" page. I want this "Link" to be unclickable like you place "#" for e.g. href="#"

Is it possible to do this using CSS or Jquery? Can't use Javascript's document.getElementById because there is no ID defined for this element.

Frank Martin
  • 2,885
  • 13
  • 47
  • 69

3 Answers3

2

Different options:

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

or

$("static dynamic-children a.menu-item").click(function(){
    return false;
});

You can also use event.preventDefault() which prevents the default/expected behaviour of a link. Use like:

$("static dynamic-children a.menu-item").click(function(event){
    event.preventDefault();
});
Sergio
  • 27,998
  • 10
  • 81
  • 130
1

Write return false

$("a.menu-item").click(function(){
    return false;
});

OR

e.preventDefault()

$("a.menu-item").click(function(e){
    e.preventDefault();
});
codingrose
  • 15,345
  • 11
  • 37
  • 58
1

You can cancel the link behavior by firing preventDefault() on the click of this link:

$("a.menu-item").click(function(evt){
    evt.preventDefault();
});
Maxime Lorant
  • 31,821
  • 17
  • 84
  • 96
dcodesmith
  • 9,445
  • 4
  • 38
  • 40