facebook@jsbanktwitter@js_bankgoogle+javascriptbanknewsletterrss@jsbank
Guest, register






ieoBIN.com - The Best IEO List to Discover, Invest in 2019

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


  • Run a function 5 times with intervals of 20 seconds
  • 1.var count = 0;
    2.function myFunction() {
    3.    count++;
    4.    if(count > 5) clearInterval(timeout);
    5.    //do something
    6.}
    7.var timeout = setInterval(myFunction, 20000);
  • Check if an element exists
  • 1.if ($("#elementid").length) {
    2.    //it does!
    3.}
  • Cancel an ajax request
  • 01.var req = $.ajax({
    02.type:     "POST",
    03.url:     "someurl",
    04.data:     "id=1",
    05.success: function(){
    06.//something
    07.}
    08.});
    09.//Cancel the Ajax Request
    10.req.abort()
  • Check if cookies are enabled
  • 01.$(document).ready(function() {
    02.    var dt = new Date();
    03.    dt.setSeconds(dt.getSeconds() + 60);
    04.    document.cookie = "cookietest=1; expires=" + dt.toGMTString();
    05.    var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1;
    06.    if(!cookiesEnabled){
    07.        //cookies are not enabled
    08.    }
    09.});
  • Toggle all checkboxes
  • 1.var tog = false; // or true if they are checked on load
    2.$('a').click(function() {
    3.    $("input[type=checkbox]").attr("checked",!tog);
    4.    tog = !tog;
    5.});
  • Get the index of an element
  • 1.$("ul > li").click(function () {
    2.    var index = $(this).prevAll().length;
    3.});
  • Validate date of birth
  • 01.$("#lda-form").submit(function(){
    02.    var day = $("#day").val();
    03.    var month = $("#month").val();
    04.    var year = $("#year").val();
    05.    var age = 18;
    06.    var mydate = new Date();
    07.    mydate.setFullYear(year, month-1, day);
    08. 
    09.    var currdate = new Date();
    10.    currdate.setFullYear(currdate.getFullYear() - age);
    11.    if ((currdate - mydate) < 0){
    12.        alert("Sorry, only persons over the age of " + age + " may enter this site");
    13.        return false;
    14.    }
    15.    return true;
    16.});
  • Test if an element is visible
  • 1.if($(element).is(":visible")) {
    2.    //The element is Visible
    3.}
  • Counting child elements
  • 01.<div id="foo">
    02.<div id="bar"></div>
    03.<div id="baz">
    04.<div id="biz">
    05.</div>
    06.<span><span>
    07.</div>
    08. 
    09.//jQuery code to count child elements
    10.$("#foo > div").length
  • Center an element on the screen
  • 01.jQuery.fn.center = function () {
    02.    this.css("position","absolute");
    03.    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    04.    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    05.    return this;
    06.}
    07. 
    08.//Use the above function as:
    09.$(element).center();

    JavaScript by day


    Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web