Activity Forums Salesforce® Discussions Loop Syntax in jQuery

Tagged: , , ,

  • Loop Syntax in jQuery

    Posted by Sourabh on April 13, 2016 at 8:17 am

    jQuery 3.0 is the most newest version of jQuery. There are many changes in it, but one of the important change
    is the syntax of loop using for..of loop.

    For example earlier we use the syntax shown below:-

    var $divelements = $("div");
    for (var x=0; x< $divelements.length; x++){
    $divelements[x].addClass("dummy");
    }

    Here's the new syntax:-

    var $divelements = $("div");
    var i = 0;
    for(var elm of $divelements) {
    elm.addClass("dummy");
    }

    With the help of this new syntax, you will get DOM elements, instead of jQuery collection. You not need to use index to get element, it will be directly accessible to you.

    • This discussion was modified 8 years, 7 months ago by  Sourabh.
    • This discussion was modified 8 years, 6 months ago by  Forcetalks.
    Prafull replied 8 years, 7 months ago 2 Members · 1 Reply
  • 1 Reply
  • Prafull

    Member
    April 16, 2016 at 11:16 am

    You can also do the same as below :-

    $("div").each(function(index) {
    $(this).addClass("dummy");
    });

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos