Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <h1>SIMPLE LIKE/DISLIKE BUTTON</h1>
  <div id="demoA"></div>
  <div id="demoB"></div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/like-dislike-button/" target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) WRAPPER */
.lidiWrap {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 120px;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 10px;
  background: #f3f3f3;
}
.lidiWrap.count { width: 250px; }

/* (B) LIKE-DISLIKE BUTTONS */
.lidiUp, .lidiDown {
  width: 24px; height: 24px;
  background-image: url(https://code-boxx.com/wp-content/uploads/2021/08/dis-like.png);
  cursor: pointer;
}
.lidiDown { background-position: -48px; }
.lidiUp.set { background-position: -24px; }
.lidiDown.set { background-position: -72px; }

/* (C) LIKE-DISLIKE COUNT */
.lidiUpCount, .lidiDownCount {
  font-size: 18px;
  color: #858585;
}

/* (X) DOES NOT MATTER */
* {
  font-family: arial;
  box-sizing: box-border;
}
body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(https://images.unsplash.com/photo-1523821741446-edb2b68bb7a0?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NjE0MTczMDU&ixlib=rb-1.2.1&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
.widget-wrap {
  width: 800px; 
  padding: 20px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.3);
}
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  border: 0;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                function lidi (inst) {
//  hWrap : html like/dislike container
//  change : function to handle like/dislike toggle change
//  status : -1 dislike, 0 neutral, 1 like (optional, default 0)
//  count : [likes, dislikes] (optional)

  // (A) DEFAULT STATUS + CSS WRAPPER CLASS
  if (!inst.status) { inst.status = 0; }
  inst.hWrap.classList.add("lidiWrap");

  // (B) ATTACH LIKE & DISLIKE BUTTON
  inst.hUp = document.createElement("div");
  inst.hDown = document.createElement("div");
  inst.hUp.className = "lidiUp";
  inst.hDown.className = "lidiDown";
  if (inst.status==1) { inst.hUp.classList.add("set"); }
  if (inst.status==-1) { inst.hDown.classList.add("set"); }
  inst.hWrap.appendChild(inst.hUp);
  inst.hWrap.appendChild(inst.hDown);

  // (C) ATTACH LIKE & DISLIKE COUNT
  if (inst.count) {
    // (C1) LIKE & DISLIKE COUNT HTML
    inst.hUpCount = document.createElement("div");
    inst.hDownCount = document.createElement("div");
    inst.hUpCount.className = "lidiUpCount";
    inst.hDownCount.className = "lidiDownCount";
    inst.hUpCount.innerHTML = inst.count[0];
    inst.hDownCount.innerHTML = inst.count[1];
    inst.hWrap.classList.add("count");
    inst.hWrap.insertBefore(inst.hUpCount, inst.hDown);
    inst.hWrap.insertBefore(inst.hDownCount, inst.hDown.nextSibling);

    // (C2) UPDATE LIKE & DISLIKE COUNT
    inst.recount = count => {
      inst.count = count;
      inst.hUpCount.innerHTML = count[0];
      inst.hDownCount.innerHTML = count[1];
    };
  }

  // (D) TOGGLE LIKE/DISLIKE
  inst.updown = up => {
    // (D1) UPDATE STATUS FLAG
    if (up) { inst.status = inst.status == 1 ? 0 : 1; }
    else { inst.status = inst.status == -1 ? 0 : -1; }

    // (D2) UPDATE LIKE/DISLIKE CSS
    if (inst.status==1) {
      inst.hUp.classList.add("set");
      inst.hDown.classList.remove("set");
    } else if (inst.status==-1) {
      inst.hUp.classList.remove("set");
      inst.hDown.classList.add("set");
    } else {
      inst.hUp.classList.remove("set");
      inst.hDown.classList.remove("set");
    }

    // (D3) TRIGGER CHANGE
    inst.change(inst.status);
  };
  inst.hUp.onclick = () => { inst.updown(true); };
  inst.hDown.onclick = () => { inst.updown(false); };

  // (E) ENABLE/DISABLE
  inst.enable = () => {
    inst.hUp.onclick = () => { inst.updown(true); };
    inst.hDown.onclick = () => { inst.updown(false); };
  };
  inst.disable = () => {
    inst.hUp.onclick = "";
    inst.hDown.onclick = "";
  };
  inst.enable();
  
  // (F) DONE
  return inst;
}

// (G) ATTACH LIKE DISLIKE BUTTON
window.onload = () => {
  // (G1) BAREBONES VERSION
  var demoA = lidi({
    hWrap : document.getElementById("demoA"),
    change : status => { console.log(status); }
  });

  // (G2) WITH COUNT
  var demoB = lidi({
    hWrap : document.getElementById("demoB"),
    status : 1,
    count : [123, 456],
    change : status => {
     console.log(status); // send the new status to update your server
     demoB.recount([654, 321]); // to update the like/dislike count
    }
  });
};
              
            
!
999px

Console