Declare the following HTML
![image image](http://lh3.ggpht.com/_0j4bzarlOBg/TaBMkS0N44I/AAAAAAAAB_E/mXHYUP2J0rA/image%5B4%5D.png?imgmax=800)
Now in the <head> section, write the following code:
<head> <title>Read ID attribute (from DevCurry.com)</title> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.5.2.min.js"> </script> <script type="text/javascript"> $(function () { var ids = $("input[type=text]") .map(function () { return this.id; }).get(); alert(ids); }); </script> </head>
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. OUTPUT
![image image](http://lh3.ggpht.com/_0j4bzarlOBg/TaBMllIM9TI/AAAAAAAAB_I/eHvcqpsB90k/image%5B7%5D.png?imgmax=800)
See a Live Demo
Tweet
No comments:
Post a Comment