Monday, 22 April 2019

remove a property from a JavaScript object?


remove a property from a JavaScript object?

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];

What does “use strict” do in JavaScript?


What does “use strict” do in JavaScript?

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
And:

Strict mode helps out in a couple ways:

It catches some common coding bloopers, throwing exceptions.
It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
It disables features that are confusing or poorly thought out.

Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function

// Non-strict code...

(function(){
"use strict";

// Define your library strictly...
})();

// Non-strict code...

check whether a string contains a substring in JavaScript?


check whether a string contains a substring in JavaScript?

var string = "foo",
substring = "oo";
string.includes(substring)

includes doesn’t have IE support, though. In an ES5 or older environment, String.prototype.indexOf, which returns −1 when it doesn’t find the substring, can be used instead:

var string = "foo",
substring = "oo";

string.indexOf(substring) !== -1

remove a particular element from an array in JavaScript?


remove a particular element from an array in JavaScript?

var array = [2, 5, 9];
console.log(array)
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}

// array = [2, 9]
console.log(array);

How JavaScript closures work?


How JavaScript closures work?

function sayHello(name) {
var text = 'Hello ' + name;
var say = function() { console.log(text); }
say();
} sayHello('lisa');

can I know which radio button is selected via jQuery?


can I know which radio button is selected via jQuery?

$('#myForm input').on('change', function() {
alert($('input[name=radioName]:checked', '#myForm').val());
});

how use transparent background using CSS?


use transparent background using CSS?

background-color:rgba(255,0,0,0.5);

how Change an HTML5 input's placeholder color with CSS


now you can Change an HTML5 input's placeholder color with CSS

::-webkit-input-placeholder { /* WebKit, Blink, Edge */
color: #909;
} :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: #909;
opacity: 1;
} ::-moz-placeholder { /* Mozilla Firefox 19+ */
color: #909;
opacity: 1;
} :-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #909;
} ::-ms-input-placeholder { /* Microsoft Edge */
color: #909;
} ::placeholder { /* Most modern browsers support this now. */
color: #909;
}

transition height: 0; to height: auto; using CSS?


transition height: 0; to height: auto; using CSS?

#menu #list {
max-height: 0;
transition: max-height 0.15s ease-out;
overflow: hidden;
background: #d5d5d5;
}

#menu:hover #list {
max-height: 500px;
transition: max-height 0.25s ease-in;
}

how CSS triangles work?


CSS triangles work?

#triangle-up {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
}

vertically center text with CSS?


vertically center text with CSS?

#box {
height: 170px;
width: 270px;
background: #000;
font-size: 48px;
color: #FFF;
text-align: center;
}

change the href for a hyperlink using jQuery???


change the href for a hyperlink using jQuery???


$("a").attr("href", "http://www.google.com/")


modify the href of all hyperlinks to point to Google. For instance, if you have a mix of link source (hyperlink) and link target (a.k.a. "anchor") anchor tags:

The yahoo

How do I check if an element is hidden in jQuery?


How do I check if an element is hidden in jQuery?
// Checks css for display:[none|block], ignores visibility:[true|false] $(element).is(":visible");
// The same works with hidden
$(element).is(":hidden");

embed an SVG and change its color in CSS without using a JS-SVG framework?


embed an SVG and change its color in CSS without using a JS-SVG framework?

jQuery('img.svg').each(function(){ var $img = jQuery(this); var imgID = $img.attr('id'); var imgClass = $img.attr('class'); var imgURL = $img.attr('src'); jQuery.get(imgURL, function(data) { // Get the SVG tag, ignore the rest var $svg = jQuery(data).find('svg'); if(typeof imgID !== 'undefined') { $svg = $svg.attr('id', imgID); } if(typeof imgClass !== 'undefined') { $svg = $svg.attr('class', imgClass+' replaced-svg'); } $svg = $svg.removeAttr('xmlns:a'); $img.replaceWith($svg); }, 'xml'); }); 

Thursday, 18 April 2019

refresh a page with jQuery

here we provide code for refresh a page with jQuery $('#something').click(function() {
    location.reload();
});



The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the browser's cache.

How to change color of SVG image using CSS


First, use an IMG tag in your HTML to embed an SVG graphic. I used Adobe Illustrator to make the graphic.


This is just like how you'd embed a normal image. Note that you need to set the IMG to have a class of svg. The 'social-link' class is just for examples sake. The ID is not required, but is useful.

Then use this jQuery code (in a separate file or inline in the HEAD).

    /*
     * Replace all SVG images with inline SVG
     */
        jQuery('img.svg').each(function(){
            var $img = jQuery(this);
            var imgID = $img.attr('id');
            var imgClass = $img.attr('class');
            var imgURL = $img.attr('src');

            jQuery.get(imgURL, function(data) {
                // Get the SVG tag, ignore the rest
                var $svg = jQuery(data).find('svg');

                // Add replaced image's ID to the new SVG
                if(typeof imgID !== 'undefined') {
                    $svg = $svg.attr('id', imgID);
                }
                // Add replaced image's classes to the new SVG
                if(typeof imgClass !== 'undefined') {
                    $svg = $svg.attr('class', imgClass+' replaced-svg');
                }

                // Remove any invalid XML tags as per http://validator.w3.org
                $svg = $svg.removeAttr('xmlns:a');

                // Replace image with new SVG
                $img.replaceWith($svg);

            }, 'xml');

        });


What the above code does is look for all IMG's with the class 'svg' and replace it with the inline SVG from the linked file. The massive advantage is that it allows you to use CSS to change the color of the SVG now, like so:


The jQuery code I wrote also ports across the original images ID and classes. So this CSS works too:

remove a property from a JavaScript object?

remove a property from a JavaScript object? delete myObject.regex; // or, delete myObject['regex']; // or, var prop = ...