Re: [jQuery] Beginner question re: getting and setting element class name
Thanks, and I already took your advice :) On 1/14/07, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote: Just a bit clunky. To me clunky is a overly long calling sequence this or this.id is easy to pass. "#"+this.id is more clunky when you put the more clunky expressions inside a function you can re-used 'proven' code. Just the advice from an seasoned(salty) programmer! -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
Just a bit clunky. To me clunky is a overly long calling sequence this or this.id is easy to pass. "#"+this.id is more clunky when you put the more clunky expressions inside a function you can re-used 'proven' code. Just the advice from an seasoned(salty) programmer! "the less you type in your programs , the less that can go wrong!" On 1/14/07, Miles Storey <[EMAIL PROTECTED]> wrote: > I can pass 'this' but using ("#"+this.id) was the way to do it with the > least code written. Inside the function I need to check the first 4 letters > of the ID string of the calling element to determine how to proceed, as well > as use the id as an element reference for the jQuery object. So it's either > pass that or use 'this' and inside the function: > > checkid = $(id).attr("id").substring(0,4) > > and then use checkid in comparisons. But I thought creating the extra object > inside the function to carry this was wasteful. > > Since passing the concated '#id' string works without further modification > inside the function it seemed like the best way to go about it. I'm not a > particularly skilled JS developer though, so perhaps the best practice is to > keep shenanigans like that inside the function? If it's hackish perhaps I'll > find it doesn't always work in the future. > > Cheers > Miles > > > > On 1/14/07, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote: > > a bit clunky! just pass in this! and let checkState do the dirty > > work... if it is actually needed! > > > > > > -- > > Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ > > ___ > > jQuery mailing list > > discuss@jquery.com > > http://jquery.com/discuss/ > > > > > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > > > -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
I can pass 'this' but using ("#"+this.id) was the way to do it with the least code written. Inside the function I need to check the first 4 letters of the ID string of the calling element to determine how to proceed, as well as use the id as an element reference for the jQuery object. So it's either pass that or use 'this' and inside the function: checkid = $(id).attr("id").substring(0,4) and then use checkid in comparisons. But I thought creating the extra object inside the function to carry this was wasteful. Since passing the concated '#id' string works without further modification inside the function it seemed like the best way to go about it. I'm not a particularly skilled JS developer though, so perhaps the best practice is to keep shenanigans like that inside the function? If it's hackish perhaps I'll find it doesn't always work in the future. Cheers Miles On 1/14/07, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote: a bit clunky! just pass in this! and let checkState do the dirty work... if it is actually needed! -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
a bit clunky! just pass in this! and let checkState do the dirty work... if it is actually needed! On 1/13/07, Miles Storey <[EMAIL PROTECTED]> wrote: > > > On 1/14/07, Karl Swedberg <[EMAIL PROTECTED]> wrote: > > > > > > Is "id" an ID? If so, you could use $('#id.something') and chain > > whatever methods you want onto it. > > The code that sets the event and function call is: > > $("#options").find("a").click(function(){ > checkState("#"+this.id)}); > > If there's a better way to pass an element reference to a function I'd love > to know, this works but it feels a bit clunky having to append the # like > that. > > > IYou could do something like this: > > > >$(id).removeClass('inactive').addClass('active'); > > > > and this: > > > >$(id).removeClass('active').addClass('inactive'); > > > > Your approach will strip the id of any other classes that it might > > have as well. So, if the element were , you would be changing it to , > > and you would be losing the "pretty." > > > > By using .removeClass() and .addClass() you can be a little more > > precise. > > Thanks, I thought about that, it is probably better practice than the way > I'm doing it using the attr method. > > Cheers > Miles > > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > > > -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
On 1/14/07, Karl Swedberg <[EMAIL PROTECTED]> wrote: Is "id" an ID? If so, you could use $('#id.something') and chain whatever methods you want onto it. The code that sets the event and function call is: $("#options").find("a").click(function(){checkState("#"+this.id)}); If there's a better way to pass an element reference to a function I'd love to know, this works but it feels a bit clunky having to append the # like that. IYou could do something like this: $(id).removeClass('inactive').addClass('active'); and this: $(id).removeClass('active').addClass('inactive'); Your approach will strip the id of any other classes that it might have as well. So, if the element were , you would be changing it to , and you would be losing the "pretty." By using .removeClass() and .addClass() you can be a little more precise. Thanks, I thought about that, it is probably better practice than the way I'm doing it using the attr method. Cheers Miles ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
Thanks! I didn't know that existed, I assume it returns boolean. That's great! Cheers On 1/14/07, Mike Alsup <[EMAIL PROTECTED]> wrote: > function checkState(id) { > if ($(id).className == "something") { etc Use the "is" method: $(id).is("something"); ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
On Jan 13, 2007, at 9:52 PM, Miles Storey wrote: > Hi, I'm just starting to use jQuery and, while I'm not much of a > coder, I love the way it works. I was developing a project using > the Prototype library, but after discovering jQuery I'm converting > everything. I'm finding it straightforward but I have a request for > a clarification on something. > > I have a function that branches depending on the class name of the > element that was passed to it. > > Using Prototype: > > function checkState(id) { > if ($(id).className == "something") { etc > > but in jQuery it doesn't work. It does work if I use: > > if ($(id).attr("class") == ""something") { etc > > I'm just wondering, as a beginner, if that would be the most > 'correct' way to do it. Is "id" an ID? If so, you could use $('#id.something') and chain whatever methods you want onto it. > Also, if I want to toggle the class name of an element between say > "active" and "inactive" what is the easiest way to do that? From > the documentation I see that addClass won't replace the existing > class so it looks like the best way is to use the attr(key,value) > method and do something like: > > $(id).attr("class", "active") > > It works, I'm just looking for 'best practise' advice. IYou could do something like this: $(id).removeClass('inactive').addClass('active'); and this: $(id).removeClass('active').addClass('inactive'); Your approach will strip the id of any other classes that it might have as well. So, if the element were , you would be changing it to , and you would be losing the "pretty." By using .removeClass() and .addClass() you can be a little more precise. > > Thanks > Miles > ___ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Beginner question re: getting and setting element class name
> function checkState(id) { > if ($(id).className == "something") { etc Use the "is" method: $(id).is("something"); ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
[jQuery] Beginner question re: getting and setting element class name
Hi, I'm just starting to use jQuery and, while I'm not much of a coder, I love the way it works. I was developing a project using the Prototype library, but after discovering jQuery I'm converting everything. I'm finding it straightforward but I have a request for a clarification on something. I have a function that branches depending on the class name of the element that was passed to it. Using Prototype: function checkState(id) { if ($(id).className == "something") { etc but in jQuery it doesn't work. It does work if I use: if ($(id).attr("class") == ""something") { etc I'm just wondering, as a beginner, if that would be the most 'correct' way to do it. Also, if I want to toggle the class name of an element between say "active" and "inactive" what is the easiest way to do that? From the documentation I see that addClass won't replace the existing class so it looks like the best way is to use the attr(key,value) method and do something like: $(id).attr("class", "active") It works, I'm just looking for 'best practise' advice. Thanks Miles ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/