Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 16th, 2012, 05:35 PM
Authorized User
Points: 233, Level: 4
Points: 233, Level: 4 Points: 233, Level: 4 Points: 233, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 44
Thanks: 14
Thanked 0 Times in 0 Posts
Default

In the Note on the top of page 376, you say that since all jQuery selectors return a collection of items, "you need to index the collection".

Okay, but I cannot understand why

$("#Button1").get(0).value = "Click Me"; // ------> it works
$("#Button1").value = "Click Me"; // ------> it doesn't work


whereas just a few lines before, on the bottom of page 375

$("#Button1").get(0).addClass("NewClassName"); // ------> it doesn't work
$("#Button1").addClass("NewClassName"); // ------> it works

Thanks for your help
Antonius

Last edited by Antonius Block; March 16th, 2012 at 05:49 PM..
  #2 (permalink)  
Old March 17th, 2012, 04:17 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I think your confusion is caused by the fact that it is unclear what is a jQuery matched set and what is a single JavaScript object. Many jQuery methods return a matched set on which you can operate. Stuff like addClass works on the entire matched set (and basically loops over the items in the set and applies the class).

However, not all JavaScript properties (such as the value of a text box) become available on the matched set. They have to be supported by jQuery. So .value doesn't work, while val() does as it has been implemented by jQuery.

Finally, a single, JavaScript object (taken out of the matched set), supports its own properties, but no JjQuery methods anymore.

With that in mind, I hope the following comments explain why something does or doesn't work:

Code:
 
$("#Button1").get(0).value = "Click Me"; // it works; single object, no matched set; 
                                         // this object just happens to have a value property.
 
$("#Button1").value = "Click Me"; // it doesn't work because the matched set 
                                  // doesn't have a value property or method so it doesn't 
                                  // know what to with it.
 
$("#Button1").get(0).addClass("NewClassName"); // it doesn't work because addClass is 
                                               // a jQuery method defined for the matched set, 
                                               // while get(0) returns a single JavaScript object
 
$("#Button1").addClass("NewClassName"); // it works, because the matched set has an 
                                        // addClass method. It loops over all elements in the 
                                        // collection (in this case there should be only one) and 
                                        // applies the CSS class you specify.
Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with jquery ch 11 nzg5 BOOK: Beginning ASP.NET 4 : in C# and VB 16 February 25th, 2012 10:49 PM
Problem with jquery ch 11 nzg5 BOOK: Beginning ASP.NET 4 : in C# and VB 1 October 4th, 2010 11:57 AM
JQuery Troubles- Chapter 11, Exercise 1 Princess Rose BOOK: Beginning ASP.NET 4 : in C# and VB 6 May 3rd, 2010 05:35 PM
Article: Using the jQuery Selectors API jminatel BOOK: Beginning JavaScript and CSS Development with jQuery 0 April 20th, 2009 11:58 AM
return collection by function in class jimusa Visual Basic 2005 Basics 1 July 30th, 2008 03:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.