skip to Main Content

How to add and remove the same class using pure Js? Add class on "mouseover" event, delete on "mousedown" event.

HTML

    <section class="preview">
        <div class="container">
            <div class="preview__wrapper">
                <div class="preview__text">
                    <h1>Navigating the digital landscape for success</h1>
                    <div class="preview__descrp">Our digital marketing agency helps businesses grow and succeed online through a range of services including SEO, PPC, social media marketing, and content creation.</div>
                    <button onmouseover="result(1)" onmousedown="result(1)"  class="btn btn_prev">Book a consultation</button> // event button!
                </div>
            </div>
        </div>
    </section>

SCSS


.btn{
    width: 231px;
    height: 68px;
    border-radius: 14px;
    border-color: #191A23;
    background-color: #fff;
    color: #000;
    font-size: 20px;
    padding: 0;
    &_prev{
        color: #fff;
        background-color: #191A23;
        width: 264px;
    }
    &_sh{
        box-shadow: 5px 5px darkgoldenrod;
    }
}

JS

let el = document.querySelectorAll('button');

function result(n){
    for (let i = 0; i < el.length; i++){
        el[n].onmouseover = el[n].onmousedown = changeButton;
        function changeButton(event){
            if(event.type =='mouseover'){
                for(let i = 0; i < el.length; i++){
                    if(!el[n].classList.contains('btn_sh')){
                        el[n].classList.add('btn_sh')
                    }
                }
            } 
            if(event.type == 'mousedown'){
                for(let i = 0; i < el.length; i++){
                    if(el[n].classList.contains('btn_sh')){
                        el[n].classList.remove('btn_sh')
                    }
                }   
            }
        }
    }
   
}

2

Answers


  1. I have made some changes to your code, pls try this way

    js file

    let el = document.querySelectorAll('button');
    
    function result(n, eventType){
        for (let i = 0; i < el.length; i++) {
          if(eventType === 'mouseover'){
              if(!el[n].classList.contains('btn_sh')){
                 el[n].classList.add('btn_sh')
               }
          } 
          if(eventType === 'mousedown'){
              if(el[n].classList.contains('btn_sh')){
                 el[n].classList.remove('btn_sh')
              }  
          }
        }
    }
    

    Html file

    <section class="preview">
            <div class="container">
                <div class="preview__wrapper">
                    <div class="preview__text">
                        <h1>Navigating the digital landscape for success</h1>
                        <div class="preview__descrp">Our digital marketing agency helps businesses grow and succeed online through a range of services including SEO, PPC, social media marketing, and content creation.</div>
                        <button onmouseover="result(0, 'mouseover')" onmousedown="result(0, 'mousedown')"  class="btn btn_prev">Book a consultation</button> // event button!
                    </div>
                </div>
            </div>
        </section>
    
    Login or Signup to reply.
  2. You don’t need a JS here, just use .btn:hover:not(:active) CSS selector.

    That means the style is active on the mouse hover but not when you click the button.

    .btn{
        width: 231px;
        height: 68px;
        border-radius: 14px;
        border-color: #191A23;
        background-color: #fff;
        color: #000;
        font-size: 20px;
        padding: 0;
    }
    .btn_prev{
       color: #fff;
        background-color: #191A23;
        width: 264px
    }
    .btn:hover:not(:active){
        box-shadow: 5px 5px darkgoldenrod;
    }
    <section class="preview">
            <div class="container">
                <div class="preview__wrapper">
                    <div class="preview__text">
                        <h1>Navigating the digital landscape for success</h1>
                        <div class="preview__descrp">Our digital marketing agency helps businesses grow and succeed online through a range of services including SEO, PPC, social media marketing, and content creation.</div>
                        <button class="btn btn_prev">Book a consultation</button> // event button!
                    </div>
                </div>
            </div>
        </section>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search