April 15, 2019

Map - Jquery

jQuery map() function is useful when you want to verify that a list (unordered, ordered) are in the right order. Simple create the jQuery object as a Map array and then join it.

Sample Data

<div>
      <label for="two">2</label>
      <input type="checkbox" value="2" id="Boston" name="number[]">
    </div>
    <div>
      <label for="four">4</label>
      <input type="checkbox" value="4" id="Concord" name="number[]">
    </div>
    <div>
      <label for="six">6</label>
      <input type="checkbox" value="6" id="Stow" name="number[]">
    </div>
    <div>
      <label for="eight">8</label>
      <input type="checkbox" value="8" id="Carlisle" name="number[]">
</div>

Your jQuery Action

$( ":checkbox" )
  .map(function() {
    return this.id;
  })
  .get()
  .join();

Your Results would be:

"Boston,Concord,Stow,Carlisle"

Javascript results:

 return $( ":checkbox" )
  .map(function() {
    return this.id;
  })
  .get()
  .join()
  .includes("Boston,Concord,Stow,Lowell")

Comments

Add your Comments

Feel free to leave a comment about this post.

- Feel Free to add HTML to your comment!