3

I am trying to create a dynamic calendar. My first step is to add classes on to certain elements on click But, I ran into a problem along the way, classList.add(''); isnt working in the HTML. I could be doing something wrong I am not sure.

<ul class="days">
  <li onClick='#'>1<li>
  <li onclick="this.classList.add('active')">2</li>
  <li onClick='#'>3</li>
  <li onClick='#'>4</li>
  <li onClick='#'>5</li>
  <li onClick='#'>6</li>
  <li onClick='#'>7</li>
  <li onClick='#'>8</li>
  <li onClick='#'>9</li>
  <li onClick='#'><span class="active">10</span></li>
</ul>

CSS

  /* Highlight the "current" day */
   .days li .active {
      padding: 5px;
      background: #1abc9c;
      color: white 
   }

I am trying to apply the onclick event to the number 2. But classList.add(''); isnt working. I'm sure theres a reason... any help would be appreciated.

7
  • 1
    It works for me. How are you testing to check if the class is added or not? Commented May 3, 2019 at 12:32
  • @MaheerAli I have alot more of the html, this is just a small snippet of it. I am using my browser to check the onClick function, if it added the class then the background color should change Commented May 3, 2019 at 12:34
  • @MaheerAli I'll add the CSS Commented May 3, 2019 at 12:34
  • It seems to be working fine, though there are some html syntax errors. 1: you are not closing the first li. 2: you are using lower case onclick for the second li. It would also be easier to test it if you had used a snippet instead of plain code section. Commented May 3, 2019 at 12:35
  • Class should add. may be your css not applying. Commented May 3, 2019 at 12:35

4 Answers 4

2

It does not work because of your CSS rule or your DOM.

.days li .active applies the style on a child node of <li> with the class active, but using this.classList.add('active'), you add the class active on the <li> tag and the CSS rule should be .days li.active.

Sign up to request clarification or add additional context in comments.

2 Comments

thankyou. This was a perfect fix! Cheers! I will accept your answer as soon as I can
@DatBoiTrump Glad it helped ;) CSS can be tricky, one space can be troublesome!
1

Your css selector is wrong, you need to remove the space between li and .active

like

.days li.active {
  padding: 5px;
  background: '#1abc9c';
  color: white 
}

Comments

0

First of all your html is wrong for the first li element.

Second I think that maybe you are being misled by your css selector, since the 10th li element has a span inside whilst the 2nd doesn't. Check out this fiddle

https://jsfiddle.net/h6w7kaoy/1/

HTML:

 <ul class="days">
      <li onClick='#'>1</li>
      <li onClick="this.classList.add('active')">2</li>
      <li onClick='#'>3</li>
      <li onClick='#'>4</li>
      <li onClick='#'>5</li>
      <li onClick='#'>6</li>
      <li onClick='#'>7</li>
      <li onClick='#'>8</li>
      <li onClick='#'>9</li>
      <li onClick='#'><span class="active">10</span></li>
    </ul>

CSS:

li.active, li span.active {
  color: blue;
  font-weight: bold;
}

If you try to remove li.active from the css selector, you will see that clicking on the li element will not affect the visuals (but the class is still being added ;-) )

Comments

0

The problem is not with classList the problem is the selector. .days li .active will select all the elements with class active which are inside <li> and li is inside element with class .days. There shouldn't be any space b/w li and .active

.days li.active

Comments

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.