twitter@js_bankfacebook@jsbankrss@jsbank






Efficient and Helpful JavaScript/jQuery Tips and Tricks This free HTML JavaScript tutorial provides 45+ JavaScript/jQuery tips and tricks that help you work in JavaScript tasks better, through live HTML JavaScript example codes. List of some JavaScript tips and tricks:

- Refreshing the src of an image with jQuery?
- Check if an image is loaded or not with jQuery
- Remove selected text after mouse double click JavaScript event
- Validate an email address
- Check if an HTML control element exists
- Cancel an AJAX request
- Select all HTML checkboxes
- Selecting root element of a certain level in the document
- Searching a string in jQuery

Please go to the full post page for full detailed tips and HTML JavaScript example codes, then try some JavaScript tutorials:

- 10 Small Javascript Tricks You Should Master
- Some Basic Tips and Tricks for Speeding Up JavaScript
- 10 jQuery and JavaScript Tips and Tricks to Improve Your Code
- 5 Good and Small JavaScript Tips and Tricks


Label: JavaScript event, HTML control, HTML JavaScript example code, 45

Generate your business videos by AI with voice or just text



Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
Free UNLMITED AI Video App in Your Hand
  • How to transform a number like 12343778 into 12.343.778
  • 1.<div id="result">12343778</div>
    1.var delimiter = '.';
    2.$('#result').html()
    3.            .toString()
    4.            .replace(new RegExp("(^\\d{"+($this.html().toString().length%3||-1)+"})(?=\\d{3})"),"$1" + delimiter)
    5.            .replace(/(\d{3})(?=\d)/g,"$1" + delimiter);
  • Count number of textarea lines
  • 1.var text = $("#textareaId").val();
    2.var lines = text.split(/\r|\r\n|\n/);
    3.alert(lines.length);
  • Logging to the firebug console
  • 1.jQuery.fn.log = function (msg) {
    2.    console.log("%s: %o", msg, this);
    3.    return this;
    4.};
    5.$('#some_div').find('li.source > input:checkbox').log("sources to uncheck").removeAttr("checked");
  • Find X/Y of an HTML element with Javascript
  • 02.var getCumulativeOffset = function (obj) {
    03.    var left, top;
    04.    left = top = 0;
    05.    if (obj.offsetParent) {
    06.        do {
    07.            left += obj.offsetLeft;
    08.            top  += obj.offsetTop;
    09.        } while (obj = obj.offsetParent);
    10.    }
    11.    return {
    12.        x : left,
    13.        y : top
    14.    };
    15.};
  • Validate Credit Card
  • 01.function isCreditCard( CC ){
    02.    if (CC.length > 19)
    03.        return (false);
    04. 
    05.    sum = 0; mul = 1; l = CC.length;
    06.    for (i = 0; i < l; i++){
    07.        digit = CC.substring(l-i-1,l-i);
    08.        tproduct = parseInt(digit ,10)*mul;
    09.        if (tproduct >= 10)
    10.            sum += (tproduct % 10) + 1;
    11.        else
    12.            sum += tproduct;
    13.        if (mul == 1)
    14.            mul++;
    15.        else
    16.            mul-;
    17.    }
    18.    if ((sum % 10) == 0)
    19.        return (true);
    20.    else
    21.        return (false);
    22.}
  • Distinguish left and right mouse click
  • 1.$("#element").live('click', function(e) {
    2.    if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) {
    3.        alert("Left Button");
    4.    }
    5.    else if(e.button == 2)
    6.        alert("Right Button");
    7.});
  • How to get the native image size
  • 1.var img = $('#imageid');
    2.var theImage = new Image();
    3.theImage.src = img.attr("src");
    4.alert("Width: " + theImage.width);
    5.alert("Height: " + theImage.height);

    Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web