jQuery to pure Javascript examples – 5

Get inner text // jQuery var text = $(“#myElm”).text(); // Pure javascript var text = document.getElementById(“myElm”).textContent; Set inner text // jQuery $(“#myElm”).text(“My text”); // Pure javascript document.getElementById(“myElm”).textContent = “My text”; Get inner HTML // jQuery…

jQuery to pure Javascript examples – 4

Add onclick event // jQuery $(“#myElm”).click(function(){ alert(“Hello world”); }); // Pure javascript document.getElementById(“myElm”).addEventListener(“click”, function() { alert(“Hello world!”); }, false); Get child node by index //jQuery var child = $(“#myElm”).eq(2); // Pure javascript var child =…

jQuery to pure Javascript examples – 3

In these examples we see how easy it is to display, hide or use the toggle without jQuery. Hide an item  // jQuery $(“#myElm”).hide(); // Pure javascript document.getElementById(“myElm”).style.display = “none”; Show an item // jQuery…

jQuery to pure Javascript examples – 2

With this post we continue the series of examples to replace jQuery with pure javascript. Add class // jQuery $(“#myElm”).addClass(“my-class”); // Pure Javascript document.getElementById(“myElm”).classList.add(“my-class”); Remove class from an element // jQuery $(“#myElm”).removeClass(“my-class”); // Pure Javascript…

Extract only numbers from a string in JavaScript

There are many ways to extract numbers from a string in JavaScript. Here’s the function we use: function ExtractOnlyNumbersFromString(txt) { var numbers = txt.match(/\d/g); return (numbers) ? numbers.join(“”) : “”; } ExtractOnlyNumbersFromString(“a 1 b 2…

Javascript | How to check sessionStorage support

var IS_SESSION_STORAGE_SUPPORTED; try { if (window.sessionStorage) { IS_SESSION_STORAGE_SUPPORTED = true; } else { IS_SESSION_STORAGE_SUPPORTED = false; } } catch (e) { IS_SESSION_STORAGE_SUPPORTED = false; } if (console) console.log(IS_SESSION_STORAGE_SUPPORTED);

Check if a string is an integer in javascript

Small function in pure Javascript to check if a string is an integer or not. function isInteger(value) { var intRegex = /^\d+$/; return intRegex.test(value); } isInteger(5); // true isInteger(undefined); // false isInteger(“1”); // true isInteger(“1.3”);…

Get query string parameters with jQuery

$.urlParam = function (name) { var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’) .exec(window.location.href); if (results == null) { return 0; } return results[1] || 0; } console.log($.urlParam(‘dotmaui’)); //cool