How does method work in jQuery?

jQueryWeb DevelopmentFront End Technology

The map method translates a set of elements in the jQuery object into another set of values in a jQuery array which may, or may not contain elements.

The following are the parameters of method:

  • callback − The function to execute on each element in the set.


You can try to run the following code to learn how to work with method:

Live Demo


      <title>jQuery Map Method</title>
      <script src = ""></script>
            var mappedItems = $("li").map(function (index) {
               var replacement = $("<li>").text($(this).text()).get(0);
               if (index == 0) {
                  // make the first item all caps
               } else if (index == 1 || index == 3) {
                  // delete the second and fourth items
                  replacement = null;
               } else if (index == 2) {
                  // make two of the third item and add some text
                  replacement = [replacement,$("<li>").get(0)];
                  $(replacement[0]).append("<b> - A</b>");
                  $(replacement[1]).append("Extra <b> - B</b>");

               // replacement will be an dom element, null,
               // or an array of dom elements
               return replacement;
         body { font-size:16px; }
         ul { float:left; margin:0 30px; color:blue; }
         #results { color:red; }

      <ul id = "results">
Published on 16-Dec-2017 14:20:52