[jQuery] .selectedIndex VS jQuery
Why doesn't $(#b6).selectedIndex = $(#s6).selectedIndex; work? (Assuming #b6 and #s6 are similar select boxes of course. Copying billing to shipping address BTW) I know it's not really how jQuery code usually works, but would like to understand why not. -- View this message in context: http://www.nabble.com/.selectedIndex-VS-jQuery-tf2806806.html#a7831040 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
$() gets you a jQuery object, so you can use it with jQuery methods etc. To access selectedIndex you need the actual element, so try: $(#b6)[0].selectedIndex = $(#s6)[0].selectedIndex; Chris ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
jazzle schrieb: Why doesn't $(#b6).selectedIndex = $(#s6).selectedIndex; work? (Assuming #b6 and #s6 are similar select boxes of course. Copying billing to shipping address BTW) I know it's not really how jQuery code usually works, but would like to understand why not. What $(#b6) returns is a jQuery object that contains one or more elements (nodes), e.g. the search result of the expression that you passed in. If you want to access DOM properties of an element you have get them out ouf the jQuery object first. This can be done via the get(n) method or with simple Array index notation: var firstElem = $(div).get(0); var secondElem = $(div)[1]; Be careful with that: It may be the case that there is no second element, so if you try to access secondElem (which is undefined in that case) it may throw an error: secondElem.className = 'foo'; // may throw an error In such cases it's better to stick to jQuery methods like each() to prevent such errors. Your example again (untestet): $(#b6).get(0).selectedIndex = $(#s6).get(0).selectedIndex; and more jQuerish (assuming that the value corresponds to the selected index): $('#b6').val( $(#s6).val() ); -- Klaus ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
Hi I followed the discussions about 'how to make jquery more popular' and I just want to point out that this is the kind of things that should be learned to newcomers in a crash course. $() is easy to understand as a steroid getElementById(), but to understand that it returns a jQuery object belongs more to the innards of jQuery. I'm using for time to time jQuery for 4 months and it is the kind things I'm avid to better understand. get(0) or [1] ok got it, but will each() work ? is there a way to filter ? Well I usually get the answer to such questions by myself, but I'd love to see it discussed on some blogs around with pros and cons, before I even really need it... Ok, maybe I should write a few stuffs by myself before to tell others what to do, but please take this as gentle suggestion, just pointing out... olivvv Klaus Hartl wrote: jazzle schrieb: Why doesn't $(#b6).selectedIndex = $(#s6).selectedIndex; work? (Assuming #b6 and #s6 are similar select boxes of course. Copying billing to shipping address BTW) I know it's not really how jQuery code usually works, but would like to understand why not. What $(#b6) returns is a jQuery object that contains one or more elements (nodes), e.g. the search result of the expression that you passed in. If you want to access DOM properties of an element you have get them out ouf the jQuery object first. This can be done via the get(n) method or with simple Array index notation: var firstElem = $(div).get(0); var secondElem = $(div)[1]; Be careful with that: It may be the case that there is no second element, so if you try to access secondElem (which is undefined in that case) it may throw an error: secondElem.className = 'foo'; // may throw an error In such cases it's better to stick to jQuery methods like each() to prevent such errors. Your example again (untestet): $(#b6).get(0).selectedIndex = $(#s6).get(0).selectedIndex; and more jQuerish (assuming that the value corresponds to the selected index): $('#b6').val( $(#s6).val() ); -- Klaus ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
Olivier Percebois-Garve schrieb: Hi I followed the discussions about 'how to make jquery more popular' and I just want to point out that this is the kind of things that should be learned to newcomers in a crash course. $() is easy to understand as a steroid getElementById(), but to understand that it returns a jQuery object belongs more to the innards of jQuery. I'm using for time to time jQuery for 4 months and it is the kind things I'm avid to better understand. get(0) or [1] ok got it, but will each() work ? is there a way to filter ? Well I usually get the answer to such questions by myself, but I'd love to see it discussed on some blogs around with pros and cons, before I even really need it... Ok, maybe I should write a few stuffs by myself before to tell others what to do, but please take this as gentle suggestion, just pointing out... The Getting Started guide mentions it, but not very prominent: http://jquery.bassistance.de/jquery-getting-started.html#find Somewhere around the example with form reset... Do you think it would help the generic newcomer if the guide has more prominent examples and explanations about this issue? -- Jörn Zaefferer http://bassistance.de ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
$() is easy to understand as a steroid getElementById(), but to understand that it returns a jQuery object belongs more to the innards of jQuery. I'm using for time to time jQuery for 4 months and it is the kind things I'm avid to better understand. Do you think it would help the generic newcomer if the guide has more prominent examples and explanations about this issue? This could be automatically checked by the debug plugin, at least when used with Firefox and Firebug. John's original debug plugin wraps each method, but you could also have getter/setter methods that warned when someone tried $().disabled = true for instance. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
Jörn Zaefferer wrote: Olivier Percebois-Garve schrieb: Hi I followed the discussions about 'how to make jquery more popular' and I just want to point out that this is the kind of things that should be learned to newcomers in a crash course. $() is easy to understand as a steroid getElementById(), but to understand that it returns a jQuery object belongs more to the innards of jQuery. I'm using for time to time jQuery for 4 months and it is the kind things I'm avid to better understand. get(0) or [1] ok got it, but will each() work ? is there a way to filter ? Well I usually get the answer to such questions by myself, but I'd love to see it discussed on some blogs around with pros and cons, before I even really need it... Ok, maybe I should write a few stuffs by myself before to tell others what to do, but please take this as gentle suggestion, just pointing out... The Getting Started guide mentions it, but not very prominent: http://jquery.bassistance.de/jquery-getting-started.html#find Somewhere around the example with form reset... Do you think it would help the generic newcomer if the guide has more prominent examples and explanations about this issue? Well yes. Generally speaking this among the things where the framework is extending the language. Chaining seemed to be one of the aspects of jquery that peoples seemed like the most, but I did not found a lot of literature about it. What is or is not possible in a chain ? how to optimize it ? When is the 'e' param in blabla(function(e){ necessary ? Documentation is great, but is kinda like an encyclopedia, I does not really explain the logic. As any framework jquery builds complex data structures in a snap, and provides useful methods to handle it. As an intermediate developer I had no problem coding rollovers before jquery, so even if the code is neater with jquery, that's not the point of my choice of it. I believe I'll have a wahoo! effect with any frameworks because I can code in 3 lines some stuffs that move but the issue to me is more about how to code in 50 lines what would have taken 500. I KNOW jquery can do it, but if I run into weird issues, I also know it may be more difficult than normal coding because I'll understand less what is happening under the hood of jquery than what is in my code. I am promoting jquery to a co-worker who has written a big Prototype based application. I could not explain him exactly what was the type of the data he was handling, nor how to do a closure to avoid conflict with prototype (what I saw in your? tooltip). It seems little code to do, but... in my company we cannot spent 3-4 hours understanding something, time needs to be justified, so he made it old school quick and dirty. I am a php/javascript coder and used to care little about typing. What I see with frameworks (cakephp for php and jquery for js) is that they lead me to manipulate more complex data structures where type become really important. Am I going off topic ? So to sum up: jquery objects - chaining - closures That's the stuffs I wanna to master in order to claim that I really feel confident with jquery. Now let's go back to your article... olivvv ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
I think the two most confusing and frustrating things for jQuery beginners (speaking as one myself) are these: 1) Needing to use the $() operator in every statement, even if you're referring to a variable which you got from a jQuery call in the first place, as SRobertJames has mentioned ( http://www.nabble.com/this-verus-%28this%29-tf2810909.html ). 2) Not being able to use standard DOM methods on elements returned by a jQuery call, even if you reduce it to one with an #id specification or an eq(0). I wasn't aware of get(0) before, and this made me feel like I had to learn an entire new language, quirks and all, to take advantage of jQuery's features. Please don't take this as snarky. I've really enjoyed working with the library, and I'm trying to be helpful. Jörn Zaefferer wrote: Olivier Percebois-Garve schrieb: Hi I followed the discussions about 'how to make jquery more popular' and I just want to point out that this is the kind of things that should be learned to newcomers in a crash course. $() is easy to understand as a steroid getElementById(), but to understand that it returns a jQuery object belongs more to the innards of jQuery. The Getting Started guide mentions it, but not very prominent: http://jquery.bassistance.de/jquery-getting-started.html#find Somewhere around the example with form reset... Do you think it would help the generic newcomer if the guide has more prominent examples and explanations about this issue? -- Jörn Zaefferer http://bassistance.de ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/.selectedIndex-VS-jQuery-tf2806806.html#a7844427 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
Well yes. Generally speaking this among the things where the framework is extending the language. Chaining seemed to be one of the aspects of jquery that peoples seemed like the most, but I did not found a lot of literature about it. What is or is not possible in a chain ? how to optimize it ? When is the 'e' param in blabla(function(e){ necessary ? Documentation is great, but is kinda like an encyclopedia, I does not really explain the logic. As any framework jquery builds complex data structures in a snap, and provides useful methods to handle it. As an intermediate developer I had no problem coding rollovers before jquery, so even if the code is neater with jquery, that's not the point of my choice of it. I believe I'll have a wahoo! effect with any frameworks because I can code in 3 lines some stuffs that move but the issue to me is more about how to code in 50 lines what would have taken 500. I KNOW jquery can do it, but if I run into weird issues, I also know it may be more difficult than normal coding because I'll understand less what is happening under the hood of jquery than what is in my code. I am promoting jquery to a co-worker who has written a big Prototype based application. I could not explain him exactly what was the type of the data he was handling, nor how to do a closure to avoid conflict with prototype (what I saw in your? tooltip). It seems little code to do, but... in my company we cannot spent 3-4 hours understanding something, time needs to be justified, so he made it old school quick and dirty. I am a php/javascript coder and used to care little about typing. What I see with frameworks (cakephp for php and jquery for js) is that they lead me to manipulate more complex data structures where type become really important. Am I going off topic ? So to sum up: jquery objects - chaining - closures That's the stuffs I wanna to master in order to claim that I really feel confident with jquery. Now let's go back to your article... olivvv Talking of this and what is needed for getjquery.org I just read an interesting article about what a JS library should provide: http://www.webstandards.org/2006/12/12/reducing-the-pain-of-adopting-a-javascript-library/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
jquery objects - chaining - closures That's the stuffs I wanna to master in order to claim that I really feel confident with jquery. The key here is that you really do need to understand javascript. jQuery is a javascript library. It uses objects and closures. It manages the dom and events. It provides effects and ajax functionality. But all of this is possible without jQuery. The beauty and power of jQuery is that it makes these things easy. But you must still understand what they are and how they work. You need to understand scoping rules, scope chains, and closures. You need to understand the DOM. You need to understand these things not because you're using jQuery but because you're programming in javascript. And if you feel like you're already at that level then dig right in and look at the jQuery source code. It is a great example of how to write good javascript and it's well-documented too. Don't treat the source like a black-box. Regarding the jQuery object. It's just an object. Like Date or Array. It encapsulates zero or more DOM nodes and lets you manipulate those nodes using the jQuery API. $ is the shorthand notation for the jQuery object. Regarding chaining - Yes, jQuery's chaining is great. But jQuery didn't invent chaining. It's just an OO programming concept that's been around for ages. You'll find that most jQuery methods return 'this' which means they return the jQuery object. That lets you keep calling more functions like this: $('.myClass').func1().func2().func3(). Without chaining you'd have to write: var jq = $('.myClass'); jq.func1(); jq.func2(); jq.func3(); That's the whole idea behind chaining. The documentation is quite good so if you want to know if a method is chainable, just look it up in the docs. If it returns a jQuery object then it's chainable. Sorry to ramble on so long. Good luck! ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] .selectedIndex VS jQuery
This needs to be post somewhere on the jQuery site and getjquery.org very prominently. Excellent post Mike (damn, man you're on a roll!). On 12/12/06, Mike Alsup [EMAIL PROTECTED] wrote: jquery objects - chaining - closures That's the stuffs I wanna to master in order to claim that I really feel confident with jquery. The key here is that you really do need to understand javascript. jQuery is a javascript library. It uses objects and closures. It manages the dom and events. It provides effects and ajax functionality. But all of this is possible without jQuery. The beauty and power of jQuery is that it makes these things easy. But you must still understand what they are and how they work. You need to understand scoping rules, scope chains, and closures. You need to understand the DOM. You need to understand these things not because you're using jQuery but because you're programming in javascript. And if you feel like you're already at that level then dig right in and look at the jQuery source code. It is a great example of how to write good javascript and it's well-documented too. Don't treat the source like a black-box. Regarding the jQuery object. It's just an object. Like Date or Array. It encapsulates zero or more DOM nodes and lets you manipulate those nodes using the jQuery API. $ is the shorthand notation for the jQuery object. Regarding chaining - Yes, jQuery's chaining is great. But jQuery didn't invent chaining. It's just an OO programming concept that's been around for ages. You'll find that most jQuery methods return 'this' which means they return the jQuery object. That lets you keep calling more functions like this: $('.myClass').func1().func2().func3(). Without chaining you'd have to write: var jq = $('.myClass'); jq.func1(); jq.func2(); jq.func3(); That's the whole idea behind chaining. The documentation is quite good so if you want to know if a method is chainable, just look it up in the docs. If it returns a jQuery object then it's chainable. Sorry to ramble on so long. Good luck! ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/