Skip to content

Instantly share code, notes, and snippets.

View bhaireshm's full-sized avatar
🐕

Bhairesh M bhaireshm

🐕
View GitHub Profile
@bhaireshm
bhaireshm / queryParamsToObject.js
Created October 12, 2020 06:55
Converting URL query parameters to object in javascript.
let search = location.search.substring(1), urlParams;
urlParams = JSON.parse(
'{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}',
function (key, value) {
return key === "" ? value : decodeURIComponent(value);
}
);
@bhaireshm
bhaireshm / toggleDivs
Last active September 27, 2022 17:03
<div class="div1 d-block">
// div content
</div>
<div class="div2 d-none">
// div content
</div>
<div class="div3 d-none">
// div content
</div>
---------------------------------
@bhaireshm
bhaireshm / checkObject.js
Last active June 16, 2021 06:35
Checks the object, if any one key's value is empty it returns false.
checkObject(data, columns) {
let obj = {};
Object.entries(data).forEach((ele, i) => {
columns.forEach(col => {
if (ele[0] == col.field || ele[0] == 'id') {
obj[ele[0]] = ele[1];
}
});
});
for (var key in obj) {
@bhaireshm
bhaireshm / setHeightForTextArea.js
Created November 24, 2020 13:14
Just call this method to set hieght to textarea automatically whenever content is changed
function setHeightTextarea () {
$("textarea")
.on("change keyup keydown paste cut", function () {
$(this).height(0).height(this.scrollHeight);
})
.find("textarea")
.change();
}
@bhaireshm
bhaireshm / addButtonToSelect2Dropdown.js
Created December 16, 2020 16:00
Add button to select2 dropdown
/**
* @param className - class name of the select tag (you can change to id)
* @param text - content for the link
*/
function addBtnToDropdown(className, text) {
var flg = 0;
$(`.${className}`).on("select2:open", function () {
flg++;
if (flg == 1) {
@bhaireshm
bhaireshm / printPretty.js
Last active January 8, 2021 05:51
Print the object's key values in aligned way.
/**
* Example Output
*
* "id : PS10140"
* "sdid : SD13112"
* "disableCrud : false"
* "newQueryParameter : true"
*/
var obj = {id: "PS10140", sdid: "SD13112", disableCrud: "false", newQueryParameter: "true"};
@bhaireshm
bhaireshm / blockBrowserBackButton.js
Last active June 22, 2021 13:04
Disable browser back button using JavaScript.
function blockBrowserBackButton() {
history.pushState(null, null, location.href);
window.onpopstate = function () {
history.go(1);
};
}
@bhaireshm
bhaireshm / isEmpty.js
Last active April 22, 2021 14:13
To check whether the value of any variable is empty.
/**
* @param data - any datatype value.
*/
function isEmpty(data) {
if (typeof data == "number" || typeof data == "boolean") return false;
if (typeof data == "undefined" || data === null) return true;
if (typeof data.length != "undefined") return data.length == 0;
let count = 0;
for (let i in data) if (data.hasOwnProperty(i)) count++;
@bhaireshm
bhaireshm / reverseStr.js
Last active September 27, 2022 16:45
Reverse a word, sentence or reverse each words in a sentence.
// @param str: string without space
function reverseStr(str){
return str.split('').reverse().join('');
}
// @param sentence: pass a sentence.
function reverseEachWordInSentence(sentence){
return sentence.split(' ').map(reverseStr).join(' ');
}
@bhaireshm
bhaireshm / AddOrDelete.js
Created June 18, 2021 09:58
Add or delete an element from an array.
/**
* @param arr: Array
* @param ele: element to be added
* @param remEle: element to be deleted
*/
function addDelEleFromArray(arr = [], ele = "", remEle = "") {
if (ele && !arr.some((a) => a == ele)) arr.push(ele);
if (remEle) return arr.filter((a) => a != remEle);
return arr;
}