-2

maybe one of you can help me. I've written a feature in javascript that adds underlining to all "a" selectors. The function is called with the "onclick" attribute. I would like to reverse the effect, i.e. remove the underscore at second click on the same button. The question is how to do it ?

HTML code:

<button type="button" class="underlineLinks" id="underlineLinks" onclick="underlineLinks()">Click</button>

JS code:

function underlineLinks() {
    const links = document.querySelectorAll("a");
    links.forEach(a => a.style.textDecoration = "underline");
}
nsog8sm43x
  • 329
  • 5
  • 14

3 Answers3

1

You can create a CSS class with the decoration underline

.class
{
    text-decoration: underline
}

and use toggle in the JS.

Toggle will add the class if it isn't applied to your link and remove the class if it is applied to your link

function underlineLinks() {
    const links = document.querySelectorAll("a");
    links.forEach(a => a.classList.toggle("class"));
}

https://jsfiddle.net/u4sxfdy5/

user3401335
  • 2,335
  • 2
  • 19
  • 32
  • i like the toggle method – ehab May 28 '20 at 18:52
  • Thank you. Thanks to your answer, I solved my problem. Now I'm wondering how to keep the state when switching to other subpages. Will this code be suitable for such a modification ? – nsog8sm43x May 28 '20 at 19:11
  • If i understand your question, you want save the state so when someone return to this page, will found the class underline or not based on last state. With this code isn't possible, because every access to the page start from beginning state(without class). You can use LocalStorage(store in the browser) to save the state ore save in your db(server side) – user3401335 May 28 '20 at 19:18
0

you could do this

function getUnderLineLinksSetter() {
  let areLinksUnderlines = false;
  return () => {
    const links = document.querySelectorAll('a');
    areLinksUnderlines = !areLinksUnderlines;
    links.forEach(a => {
      if (areLinksUnderlines) {
        a.style.textDecoration = 'initial';
      } else {
        a.style.textDecoration = 'underline';
      }
    });
  };
}

and then use the following html

<button type="button" class="underlineLinks" id="underlineLinks" onclick="getUnderLineLinksSetter()()">Click</button>

Its better to add event listeners from code and not using html inline functions by that i mean instead of setting the on click using html, you set it using javascript like this

// put this code inside a load event in the page so you make sure the button is in the dom
function getUnderLineLinksSetter() {
  let areLinksUnderlines = false;
  return () => {
    const links = document.querySelectorAll('a');
    areLinksUnderlines = !areLinksUnderlines;
    links.forEach(a => {
      if (areLinksUnderlines) {
        a.style.textDecoration = 'initial';
      } else {
        a.style.textDecoration = 'underline';
      }
    });
  };
}


document.getElementById('underlineLinks').addEventLisetner('click', getUnderLineLinksSetter())

And then remove the onclick from html

<button type="button" class="underlineLinks" id="underlineLinks">Click</button>

ehab
  • 7,162
  • 1
  • 25
  • 30
0
function underlineLinks() {
    const links = document.querySelectorAll("a");
    var toChange = "underline"
    if(links[0].style.textDecoration != "underline")) toChange = "none"
    links.forEach(a => a.style.textDecoration = toChange);
}

Probably there is an easier way but idk, I shoot my shot

Specy
  • 189
  • 2
  • 13