2

I'm trying to change the CSS of the cursor to default on a a href link of # on the menu-item-4082 "About" link below. And I don't know why this seemingly simple function doesn't want to work.

Must be something simple I'm not seeing. Is my CSS selector correct?

Or is there a different or better way to change the CSS with jQuery? What about removing the href="#" as well?

Fiddle: http://jsfiddle.net/2nbad1gc/

Function:

$("li#menu-item-4082 .not-click").css("cursor","default");

HTML

<ul id="menu-main-menu-bar">
<li id="menu-item-217" class="menu-item">
    <a href="http://example.com/">Home</a>
</li>
<li id="menu-item-4082" class="menu-item menu-item-type-custom
    menu-item-object-custom menu-item-has-children menu-item-4082
    has-dropdown not-click">
    <a href="#">About</a>
</li>
<ul class="dropdown">
    <li id="menu-item-158" class="menu-item menu-item-158">
        <a href="http://example.com/values/">Values</a>
    </li>
    <li id="menu-item-4083" class="menu-item menu-item-4083">
        <a href="http://example.com/why/">Why</a>
    </li>
</ul>
3
  • Just to point out that you forgot to include jQuery in your fiddle, so it never would have worked, even with the fix provided by Josh. Commented Jan 25, 2015 at 17:42
  • @DrewKennedy: arrgg, you're right.... fixed. Commented Jan 25, 2015 at 17:47
  • basic css stylesheet rule would make this the easiest Commented Jan 25, 2015 at 18:07

3 Answers 3

5

Is my CSS selector correct?

No, it's incorrect. It should be:

$("li#menu-item-4082.not-click a").css("cursor","default");

You were trying to select the child of li#menu-item-4082 whose class is not-click. When in fact, the li itself had the class .not-click.

Remove the space between $("li#menu-item-4082 .not-click").

As a side note, I'd suggest adding a class rather than adding inline CSS.

$("li#menu-item-4082.not-click a").addClass('default-cursor');
.default-cursor {
    cursor: default;
}

.. you could also remove the href attribute completely:

$("li#menu-item-4082.not-click a").removeAttr('href');

If you wanted to avoid jQuery completely, you could also remove the href attribute using plain JS:

Single element:

document.querySelector('#menu-main-menu-bar .not-click a').removeAttribute('href');

Multiple elements:

var anchors = document.querySelectorAll('#menu-main-menu-bar .not-click a');

Array.prototype.forEach.call(anchors, function (el, i) {
    el.removeAttribute('href');
});

or you could avoid JS and just use CSS:

li#menu-item-4082.not-click a {
    cursor: default;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, thanks for the exhaustive answer. What I eventually needed was the multiple elements function. But, the jQuery doesn't want to work in the fiddle jsfiddle.net/2nbad1gc/28 or the live site. I loaded the live site CSS file in the fiddle, but it doesn't seem to be a CSS conflict that I can see. No console errors, either.
@songdogtech It appears to work in the fiddle you provided. jsfiddle.net/07bkgqvs It's worth pointing out that not all the li elements have the class .not-click as you can see, it was working for 2 of the elements.
Ah, you're right; Sunday morning confusion. OK, looks like something is broken on the live site, but no console errors, it's a mystery to me. But all your answers are correct. Thanks!
0

Just use simple css

.not-click a{
cursor: default;
}

http://jsfiddle.net/2nbad1gc/14/

Comments

0

Your selector needs to be modified to

$('li#menu-item-4082.not-click a').css("cursor", "default");

Usually it is recommended to add a class to the HTML element that sets cursor to default rather than directly change the CSS with jQuery like this.

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.