[jQuery] Re: Form Traversal Best Practices

2009-06-19 Thread Karl Swedberg

On Jun 19, 2009, at 2:10 PM, Brandon! wrote:


So I've always had a couple questions about forms and jQuery, so I
thought I'd finally ask and see if I can get a straight answer
(they're all pretty simple).

First question: When selecting a form element by it's attribute(s),
what's the proper format?  Do I need the @ or not basically: input
[...@type=text] or input[type=text]  I know that both formats work,
but I don't know which is preferred.


Without the @. It was deprecated in 1.2 and removed in 1.3.


Second question: In regards to performance, is there a best order to
put attributes in?  inp...@name^=line_id]...@type=checkbox] vs input
[...@type=checkbox]...@name^=line_id]  Any good rule of thumb, or is
the outcome negligible?


$(':checkbox[name^=line_id]')

Not sure about performance. Probably negligible.


Third and final question: jQuery().attr('checked', true) or jQuery
().attr('checked', 'checked')?  I see it both ways, but I wasn't sure
if it makes a difference.  XHTML tells us we want checked=checked,
but I wasn't sure.


Here, .attr() is a bit of a misnomer, because it's really looking at  
DOM properties, not (x)HTML attributes. I'm pretty sure either one  
works, but I use .attr('checked', true)


Actually, when I can get away with avoiding .attr() I usually do. For  
example, I would do:


this.checked = true;

rather than:

$(this).attr('checked', true);


--Karl


Karl Swedberg
www.englishrules.com
www.learningjquery.com



[jQuery] Re: Form Traversal Best Practices

2009-06-19 Thread Brandon!

Thanks, I thought I remembered reading about deprecating one of the
two forms, I just couldn't remember which and I couldn't find the post
again.

Why :checkbox vs input[type=checkbox]?  I noticed a couple special
keywords that had colons in front, but I wasn't sure what their
purpose was.

Thanks,

Brandon

On Jun 19, 1:21 pm, Karl Swedberg k...@englishrules.com wrote:
 On Jun 19, 2009, at 2:10 PM, Brandon! wrote:



  So I've always had a couple questions about forms and jQuery, so I
  thought I'd finally ask and see if I can get a straight answer
  (they're all pretty simple).

  First question: When selecting a form element by it's attribute(s),
  what's the proper format?  Do I need the @ or not basically: input
  [...@type=text] or input[type=text]  I know that both formats work,
  but I don't know which is preferred.

 Without the @. It was deprecated in 1.2 and removed in 1.3.

  Second question: In regards to performance, is there a best order to
  put attributes in?  inp...@name^=line_id]...@type=checkbox] vs input
  [...@type=checkbox]...@name^=line_id]  Any good rule of thumb, or is
  the outcome negligible?

 $(':checkbox[name^=line_id]')

 Not sure about performance. Probably negligible.

  Third and final question: jQuery().attr('checked', true) or jQuery
  ().attr('checked', 'checked')?  I see it both ways, but I wasn't sure
  if it makes a difference.  XHTML tells us we want checked=checked,
  but I wasn't sure.

 Here, .attr() is a bit of a misnomer, because it's really looking at  
 DOM properties, not (x)HTML attributes. I'm pretty sure either one  
 works, but I use .attr('checked', true)

 Actually, when I can get away with avoiding .attr() I usually do. For  
 example, I would do:

 this.checked = true;

 rather than:

 $(this).attr('checked', true);

 --Karl

 
 Karl Swedbergwww.englishrules.comwww.learningjquery.com


[jQuery] Re: Form Traversal Best Practices

2009-06-19 Thread Matt Kruse

On Jun 19, 1:21 pm, Karl Swedberg k...@englishrules.com wrote:
 Here, .attr() is a bit of a misnomer, because it's really looking at  
 DOM properties, not (x)HTML attributes. I'm pretty sure either one  
 works, but I use .attr('checked', true)

Indeed, the code internally sets the DOM attribute rather than calling
setAttribute().
It's been this way for a long time, and it's a favorite criticism of
jQuery from some on comp.lang.javascript.
Will it ever be fixed?

 Actually, when I can get away with avoiding .attr() I usually do. For  
 example, I would do:
 this.checked = true;

jQuery should really have .prop() which would work like attr() usually
intends to, but deal only with DOM properties rather than trying to
manipulate actual attributes. Script authors usually want to deal with
properties anyway, and attr() only makes it confusing.

Matt Kruse