Monday, 22 April 2019

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 = ...