Home > front end >  adding class to current day with javascript
adding class to current day with javascript

Time:02-11

im making a calendar and i want to add a className to current day so i could highlight it but i got some troubles and i wanna please you for help

function getCurrentDayClass() {
        return day.format("DD-MM-YY") === dayjs().format("DD-MM-YY")
        //   document.getElementById().className  = "current-day"
    }
<header>
                {rowIdx === 0 && (
                    <p>
                        {day.format('ddd').toUpperCase()}
                    </p>
                )}

                <p className="calendar-day">
                    {day.format("DD")}

                </p>

            </header>

CodePudding user response:

function getCurrentDayClass() {
  if (day.format("DD-MM-YY") === dayjs().format("DD-MM-YY"))
    return "PUT_YOUR_CSS_CLASSNAME_HERE";// something like "cal-day active"
  return "calendar-day";
}
<header>
  {rowIdx === 0 && (<p>{day.format('ddd').toUpperCase()}</p>)}
  <p className={getCurrentDayClass()}>{day.format("DD")}</p>
</header>

CodePudding user response:

This solve your problem

<p className=`${getCurrentDayClass() ? 'current-day-class' : ''}`

CodePudding user response:

When you push class with id in reactJS

   document.getElementById(id).classList.add("active")

When you push class with class in reactJS

    document.getElementsByClassName("example").classList.add("active")
   
  • Related