szmerek
szmerek
Reputation Top 5%
szmerek
4 Snippets  (177th place)
Published
1 Channel
Created
3 Channels
Following
Tuesday at 10:33:44 PM
Last Visit
Aug 14, 2017
Registered
97 points  (277th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Junior Trend Maker
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer

Recent Snippets See all snippets by szmerek

public by szmerek created Aug 14, 2017  346  7  5  0

OnClick without setting an attribute of the link

//<div id="dog" >Woow woow</div>


    (function() {
        document.getElementById("dog").onclick = function() { 
            alert('fuf fuf'); 
        };
    })();																								
;

public by szmerek created Aug 20, 2017  236  0  4  0

Remove part of a string before specified character

//remove string before '&' if want to include character '&' then 'indexOf("&") + 0'
var str = "black&white";
str = str.substring(str.indexOf("&") + 1);

//or use regex
var str = "black&white";
str = /&(.+)/.exec(str)[1];

// or use .split() and .pop() 
var str = "black&white";
str = str.split("&").pop();						
;

public by szmerek created Aug 20, 2017  230  1  4  0

Check whether a string matches a regex

//RegExp allows to inject variable values into the regex string
var str = "sample1";
var re = new RegExp("^([a-z0-9]{5,})$");
if (re.test(str)) {
    console.log("valid");
} else {
    console.log("invalid");
}					
;

public by szmerek created Aug 14, 2017  215  0  5  0

Hide element(s) by class

Some examples for hiding the class.
//Ex 
//<div class="appBanner">appbanner</div> 

document.getElementsByClassName('appBanner')[0].style.visibility = 'hidden';

//change the style rules of all elements matching the class

[].forEach.call(document.querySelectorAll('.appBanner'), function (el) {
  el.style.visibility = 'hidden';
});

//If for...of is available then use

for (let el of document.querySelectorAll('.appBanner')) el.style.visibility = 'hidden';

//And the other one

Array.filter( document.getElementsByClassName('appBanner'), function(elem){ elem.style.visibility = 'hidden'; });

//and the last one

var appBanners = document.getElementsByClassName('appBanner'), i;

for (i = 0; i < appBanners.length; i += 1) {
    appBanners[i].style.display = 'none';
}																								
;