Skip to content Skip to sidebar Skip to footer

Add A Class To A Href That's Set To '#'

Is there anyway to automatically add a class to any HREF that is set to a hash (#)? I am using an auto menu in Concrete5 so can't just hard code it in unfortunately. I'm going to t

Solution 1:

Do like this,

$('a[href="#"]').addClass('className');

Here in the above code we have used attribute equals selector.

Solution 2:

You may try to use

$('a[href$="#"]').addClass('className');

Check the docs for options of attribute selector:

Attribute Contains Prefix Selector [name|="value"]
    Selects elements that have the specified attribute with a value 
    either equal to a given string or starting with that string followed 
    by a hyphen (-).

    Attribute Contains Selector [name*="value"]
    Selects elements that have the specified attribute with a 
    value containing the a given substring.

    Attribute Contains Word Selector [name~="value"]
    Selects elements that have the specified attribute with a value
    containing a given word, delimited by spaces.

    Attribute Ends With Selector [name$="value"]
    Selects elements that have the specified attribute with a 
    value ending exactly with a given string. The comparison is case sensitive.

    Attribute Equals Selector [name="value"]
    Selects elements that have the specified attribute with a 
    value exactly equal to a certain value.

    Attribute Not Equal Selector [name!="value"]
    Select elements that either don’t have the specified attribute, 
    or do have the specified attribute but not with a certain value.

    Attribute Starts With Selector [name^="value"]
    Selects elements that have the specified attribute with a 
    value beginning exactly with a given string.

    Has Attribute Selector [name]
    Selects elements that have the specified attribute, with any value.

    Multiple Attribute Selector [name="value"][name2="value2"]
    Matches elements that match all of the specified attribute filters.

Solution 3:

Try below code its easy

$('a[href*="#"]').addClass('Class');

Demo here JSFiddle

Hope it helps...

Post a Comment for "Add A Class To A Href That's Set To '#'"