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
  • 3,147
  • 16
  • 52
  • 73

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
  • 28,539
  • 11
  • 85
  • 132
1

Write return false

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

OR

e.preventDefault()

$("a.menu-item").click(function(e){
    e.preventDefault();
});
codingrose
  • 15,563
  • 11
  • 39
  • 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
  • 34,607
  • 19
  • 87
  • 97
dcodesmith
  • 9,590
  • 4
  • 36
  • 40