Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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..
Reply With Quote
  #2 (permalink)  
Old March 17th, 2012, 04:17 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:09 AM.


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