jQuery: Store ID of all TextBoxes into an Array

I was storing the ID attribute of all the textboxes on a page, into an array. Although your first response to solve a similar requirement would be to use the $().each() but there is a better way, using map(). Let me show you how.

Declare the following HTML

Now in the <head> section, write the following code:
<title>Read ID attribute (from DevCurry.com)</title>
<script type="text/javascript"

<script type="text/javascript">
$(function () {
 var ids = $("input[type=text]")
  .map(function () {
   return this.id; 

The .map() method is useful for getting/setting the value of a collection of elements, in our case the id attribute of all textboxes. this.id refers to the id attribute of the current DOM element for each iteration.


See a Live Demo

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: