-1

I have different anchor tags with href=#ids.I would like a new value of href without the anchor at the end. Meaning, I do not want the anchor to appear on the search bar. However, I would styling of the id to come in to play.

Currently, in my CSS, I have the following styling that will highlight the background of a particular section when you select, let say Spanish link:

CSS:

#Espanol:target{
     background-color: #ffa;
    -webkit-transition: all 1s linear;
}

enter image description here

I would like that to still affect the section but not have the anchor to appear in the href.

a[href='##Non-Discrimin'] {
  display: none
}
<li class="langLI" style="list-style-type:none"><a href="/Regal-en-us/non-discrimination-and-language-assistance##Non-Discrimin">English</a></li>

The reason for the two "##" is I am using coldfusion and site crashes if I just use 1 '#'. Is there another approach I can do to hide the anchors to not display? Currently it displays as follows:

/Regal-en-us/non-discrimination-and-language-assistance/#Non-Discrimin

I would like the anchor to not display at the end:

/Regal-en-us/non-discrimination-and-language-assistance/

Robert
  • 167
  • 1
  • 2
  • 14

2 Answers2

0

You cannot do this with pure CSS, you need some JS. Here is a jQuery solution :

$('a').each(function() {
  $(this).attr('href', $(this).attr('href').split('#')[0]);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="langLI" style="list-style-type:none"><a href="/Regal-en-us/non-discrimination-and-language-assistance##Non-Discrimin">English</a></li>

This will apply to all a tag, to be more specific you can change the selector.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • I will try this approach shortly and thank you for your assistance – Robert Nov 08 '17 at 18:00
  • yes it works however, in my css, if I select let say english, it will highlight a certain section of the page. it will just direct the user to that page. – Robert Nov 08 '17 at 18:03
  • @Robert sorry but i didn't get you .. can you elaborate with an example – Temani Afif Nov 08 '17 at 18:05
  • @ Temani: Yeah. So currently, the anchor goes to a particular section of the page and changes the background color of that section to yellow. I will post what I mean – Robert Nov 08 '17 at 18:09
  • @Robert yes and since we removed this anchor from the url you won't have this anymore. You want to keep it ? – Temani Afif Nov 08 '17 at 18:11
  • So there isn't a way to have those section highlighted but not have the anchor appear in the url? – Robert Nov 08 '17 at 18:12
  • @Robert i don't think so. If we remove the anchor he url will get you to the whole page and no more to a specific section so the hightlight won't work – Temani Afif Nov 08 '17 at 18:14
  • @ Temani Afif: Darn. Sorry for wasting your time but thank you for a approach to append the url. – Robert Nov 08 '17 at 18:16
  • @Robert it's ok there is no waste ;) – Temani Afif Nov 08 '17 at 18:18
0

In order to remove the matching string from the end of the href attribute-value you'll need to use JavaScript (or a back-end server-side scripting language if you prefer):

// advance apologies for the clunky name
function removeFromAttributeByString(opts) {

  // initialising an object with the default names,
  // albeit all Object key-values are false at this
  // time (but sensible defaults could be used if
  // you prefer):
  let defaults = {
      'needle': false,
      'attribute': false,
      'haystackSelector': false
    },
    haystack;

  // iterating over the keys of the opts Object if supplied,
  // or an empty Object if opts is not supplied (this is simply
  // to prevent an error message in the absence of a supplied
  // Object) using Object.keys() and Array.prototype.forEach():
  Object.keys(opts || {}).forEach(

    // an Arrow function where we supply the current Object-key
    // ('key') to the function, and set the key-value for the
    // defaults Object to be equal to the key-value supplied
    // via the opts Object passed to the function:
    key => defaults[key] = opts[key]
  );

  // here we use the Object.values() method to retrieve an
  //  array of the defaults Object's values, and iterate over
  // that array using Array.prototype.every() with the Boolean
  // argument, testing that every value is Boolean true and,
  // if so, returning true:
  if (Object.values(defaults).every(Boolean)) {

    // here we find all elements that match the supplied CSS
    // selector (defaults.haystackSelector) and then use
    // Array.from() to convert that array-like nodeList into
    // an Array:
    haystack = Array.from(
                 document.querySelectorAll( defaults.haystackSelector )
               );

    // we filter that array, using Array.prototype.filter():
    haystack.filter(

      // using an Arrow function to pass the current element of
      // the array to the function, and if the current element
      // has the supplied attribute (defauls.attribute) it's
      // retained in the returned Array, otherwise it's discarded:
      element => element.hasAttribute(defaults.attribute)

    // using Array.prototype.forEach() to iterate over the array
    // of elements retained:
    ).forEach(

      // here we set the attribute-value to the same attribute-value
      // after we remove the supplied text ('defaults.needle') from
      // attribute-value:
      element => element[defaults.attribute] = element[defaults.attribute].replace(defaults.needle, '')
    );

  }

}

removeFromAttributeByString({
  'needle': '##Non-Discrimin',
  'attribute': 'href',
  'haystackSelector': 'a'
});

function removeFromAttributeByString(opts) {
  let defaults = {
      'needle': false,
      'attribute': false,
      'haystackSelector': false
    },
    haystack;

  Object.keys(opts || {}).forEach(
    key => defaults[key] = opts[key]
  );

  if (Object.values(defaults).every(Boolean)) {

    haystack = Array.from(document.querySelectorAll(defaults.haystackSelector));
    haystack.filter(
      element => element.hasAttribute(defaults.attribute)
    ).forEach(
      element => element[defaults.attribute] = element[defaults.attribute].replace(defaults.needle, '')
    );

  }

}

removeFromAttributeByString({
  'needle': '##Non-Discrimin',
  'attribute': 'href',
  'haystackSelector': 'a'
});
<ul>
  <li class="langLI" style="list-style-type:none"><a href="/Regal-en-us/non-discrimination-and-language-assistance##Non-Discrimin">English</a></li>
</ul>

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • @ David Thomas: Thank you for your help. I will look into the references and how this code works – Robert Nov 08 '17 at 18:22
  • You're very welcome indeed; though I've read the other answer - and the comments - and I'm still not sure I appreciate precisely what you want; although from the comments to the other answer it seems you you want to remove the `id` from the `href`, but still have the `href` link to that `id` which is, unfortunately, impossible. Although if you can explain *why* you want that particular feature/function we *may* be able to solve that problem for you in a different way. But usually the `href` attribute is never really seen by the user, so I don't understand why it truly matters in this case. – David Thomas Nov 08 '17 at 18:25