0

I have the following css rule:

.headerWrapperDiv3 a{
    color:#888;
}
.headerWrapperDiv3 a:hover {
    color:#444;
}

and the following markup:

<div class='headerWrapperDiv3'>
<ul class="nav90">
    <li><a href="index.aspx" pageCatID="3" >Home</a></li>
</ul>
</div>

and I'm using this jQuery to change the color of the link once the page loads:

$(function() {
    $("a[pageCatID=3]").each(function(){
        $(this).css("color", "#00ff00");
    });
});

The jquery finds the link ok but fails to change the color.

1
  • 2
    Your code seems to be working for me: jsfiddle Commented Dec 15, 2014 at 17:54

4 Answers 4

2

Try this one (surround the value by single quotation marks):

$(function() {
    $("a[pageCatID='3']").each(function(){
        $(this).css("color", "#00ff00");
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant, thank you. There was me thinking I had a specifity problem!
2

Try the example below

 $(function() {
        $(".nav90 a").each(function(){
            $(this).css("color", "#00ff00");
        });
    })

Solution: As shown above, just target .nav90 and the a inside.

Note: If you want more specificity, adhere to the corrections made to your code by Rodrigo and you'll be just fine.

2 Comments

Given that the OP is building a nav, I would assume the <ul> will have additional <li>'s with more <a>'s in them... this would apply the change to all of the <a>'s within the <ul class="nav90">
Make a difference with page ID. it's that simple
1

Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.

$("a[pageCatID='3']").each(function() {
  $(this).css("color", "#00ff00");
});

As you see you need to suround 3 with quotation marks.

2 Comments

@NiMa: It's an attribute selector. a[pageCatID='3'] selects all anchor elements with the attribute pageCatID set to 3.
it finds the links with the attribute pageCatID set to the value of 3
0

It's not necessary to use .each, selectors apply to every object:

$(function() {
    $("a[pageCatID=3]").css("color", "#00ff00");
});

Working example: http://jsfiddle.net/mu54yjn4/

UPDATE:

For more than one property:

$(function() {
    $("a[pageCatID=3]").css({
        "color": "#00ff00",
        "background-color": "#000"
    });
});

Working example: http://jsfiddle.net/zf6fjz0q/

1 Comment

ok. now if i was to change more than just the color, ie background and font style as well would it be more efficient to use each rather than 3 selector commands?

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.