Showing posts with label Javascript. Show all posts

Check/Uncheck All Checkbox by Jquery

Just copy this function.

function InitCheckAll(checkAllSelector, checkboxSelector) {
    $(checkAllSelector).change(function() {
        var e = this;
       
        $(checkboxSelector).each(function() {
            this.checked = e.checked;
        });
    });

    $(checkboxSelector).change(function() {
        if (this.checked == true) {
            var isCheckAll = true;
            $(checkboxSelector).each(function() {
                if(!this.checked) {
                    return isCheckAll = false;
                }
            });
            $(checkAllSelector)[0].checked = isCheckAll;
        } else {
            if ($(checkAllSelector)[0].checked == true) {
                $(checkAllSelector)[0].checked = false;
            }
        }
    });
}
Then you can declare select/deselect all checkbox just a line code. Notice that the paramaters should be selector string. For example:
InitCheckAll('#yourMasterCheckBoxID', '.yourChildCheckBoxClass')

Posted in , | Leave a comment

Escape jquery selector value

Declare the escape function for jquery at first

$.escape = function(str) {
  return str.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&');
};
Example code
// the value contain special character
var searchValue = 'your-value*'; 

// this is safe with escape
var list1 = $('[value="' + $.escape(searchValue) + '"]';

// this may error without escape
var list2 = $('[value="' + searchValue + '"]'; 
Try to type the input

This is escaped value

Posted in | Leave a comment

Escape regex string javascript

Declare the escape function at first

RegExp.escape = function(string) {
  return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
And you can search special character like this code
var patern = '^^';
var regex = new RegExp(RegExp.escape(patern));
var text = 'Hello^^';
var match = text.match(regex));
Try to type the input

Try to type the patern with special character

Matched text found:

Posted in | Leave a comment

Show spinner on load image.

Click on links to see spinner on load image.

Show image 1
Show image 2
Show image 3
function changeImg(url) {
    var img = document.getElementById('previewImg');
    var spinner = document.getElementById('spinner');
    // show spinner
    spinner.style.display = '';
    img.style.display = 'none';
    img.onload = function() {
        spinner.style.display = 'none';
        this.style.display = '';
        this.onload = null;
    }
    img.src = url;
}  

Posted in | Leave a comment

Resize image and keep aspect ratio.

Width:
Height:

Javascript

function Resize(img) { var w = document.getElementById('t1').value; var h = document.getElementById('t2').value; var d = document.getElementById('d1'); d.style.width = w + 'px'; d.style.height = h + 'px'; img.style.width = 'auto'; img.style.height = 'auto'; if(img.width / img.height >= w / h) { img.style.width = d.style.width; } else { img.style.height = d.style.height; } }

Posted in | Leave a comment

Search

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.