-1

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 take a guess and say that it will be JavaScript/jQuery that will have to be used?

Thanks in advance!

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
JoshuaBrand
  • 175
  • 1
  • 14

3 Answers3

3

Do like this,

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

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

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

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.
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Try below code its easy

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

Demo here JSFiddle

Hope it helps...

Anto king
  • 1,222
  • 1
  • 13
  • 26