JS - Other

Add asterisks to required fields

$('input').each(function () {
    var req = $(this).attr('data-val-required');
    if (undefined != req) {
        var label = $('label[for="' + $(this).attr('id') + '"]');
        var text = label.text();
        if (text.length > 0) {
            label.append('<span style="color:red"> *</span>');
        }
    }
});

Auto Playing Next Video After First Video End

$("video ID/CLASS/TAG source:first").addClass("active");

var VAR = document.getElementById/Name/TagName("video ID/CLASS/TAG");

VAR.addEventListener('ended', function(e) {
 // get the active source and the next video source.
 // I set it so if there's no next, it loops to the first one
 var activesource = document.querySelector("video ID/CLASS/TAG source.active");
 var nextsource = document.querySelector("video ID/CLASS/TAG source.active + source") || document.querySelector("video ID/CLASS/TAG source:first-child");

 // deactivate current source, and activate next one
 activesource.className = "";
 nextsource.className = "active";

 // update the video source and play
 VAR.src = nextsource.src;
 VAR.play();

});

Battery Status API

The Battery Status API, more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change. This can be used to adjust your app's resource usage to reduce battery drain when the battery is low, or to save changes before the battery runs out in order to prevent data loss.


navigator.getBattery().then((battery) => {
  function updateAllBatteryInfo() {
    updateChargeInfo();
    updateLevelInfo();
    updateChargingInfo();
    updateDischargingInfo();
  }
  updateAllBatteryInfo();


  battery.addEventListener("chargingchange", () => {
    updateChargeInfo();
  });
  function updateChargeInfo() {
    console.log(`Battery charging? ${battery.charging ? "Yes" : "No"}`);
  }


  battery.addEventListener("levelchange", () => {
    updateLevelInfo();
  });
  function updateLevelInfo() {
    console.log(`Battery level: ${battery.level * 100}%`);
  }


  battery.addEventListener("chargingtimechange", () => {
    updateChargingInfo();
  });
  function updateChargingInfo() {
    console.log(`Battery charging time: ${battery.chargingTime} seconds`);
  }


  battery.addEventListener("dischargingtimechange", () => {
    updateDischargingInfo();
  });
  function updateDischargingInfo() {
    console.log(`Battery discharging time: ${battery.dischargingTime} seconds`);
  }
});

Bookmark/Add Your Webpage

IE Favorite

if (window.external) {
  window.external.AddFavorite(location.href, document.title);
}


Mozilla Firefox Bookmark

if (window.sidebar) {
  window.sidebar.addPanel(location.href, document.title, "");
}


Opera Hotlist

if (window.opera && window.print) {
  this.title = document.title;
  return true;
}

Calculate Base 10 Logarithm Of A Number

Math.log10(number)

Calculate The Length Of String

string.length

Call a function when Enter is pressed

let textArea = document.getElementById("inputOne");
textArea.addEventListener("keydown", function(e){
    if(e.key === "Enter"){
      validate(e);
    }
});

funtion validate(e){
    alert('Enter is pressed');
}

Change title when user change tab

let docTitle = document.title;

window.addEventListener("blur", () => {
  document.title = "Come back :(";
})

window.addEventListener("focus", () => {
  document.title = docTitle;
})

Check Duplicate Value by Table Row

var contents = {};
var duplicates = false;

$('table tbody tr').each(function () {
 var tdContent = $(this).children('input').val();
 if (contents[tdContent]) {
  duplicates = true;
  return false;
 }
 contents[tdContent] = true;
});

if (duplicates) {
 console.log('duplicate');
}

Check Duplicate Value in Multidimensional Array

var arr = [];
var result = [];

var frequency = arr.reduce(function (seen, currentItem) {
 if (currentItem in seen) {
  seen[currentItem] = seen[currentItem] + 1;
 } else { seen[currentItem] = 1; }
  return seen;
 }, {});

for (var key in frequency) {
 if (frequency[key] > 1) {
  result.push(key.split(',').map(function (currentItem) {
   return currentItem;
  }));
 }
}

Check For Multiple Conditions

if([A, B, C].includes(A)){
 console.log('included');
}

Check If Argument is Number

!isNaN(parseFloat(arg)) && isFinite(arg)