Re: [jQuery] Single Value Attributes

2006-10-04 Thread Brandon Aaron
I think it has already been brought up that this will not work as
expected but ... here it goes again ...

These attributes are used to set the 'default' value and are
thereafter ignored in favor of the JS object's properties. At least
that is how I understand it.

--
Brandon Aaron


On 10/2/06, John Resig [EMAIL PROTECTED] wrote:
 Hey everyone -

 I stumbled across a point of optimization today, all of the following
 attributes (and probably more - let me know) can only have a single
 value.

 checked=checked
 multiple=multiple
 disabled=disabled
 readonly=readonly
 disabled=disabled
 selected=selected

 I'd like to add in methods like:
 .checked( true | false )

 Which does this in the background (but for each attribute):

 $.fn.checked = function(b) {
 if ( b )
 this.attr( checked, checked );
 else
 this.removeAttr( checked );
 };

 What do you think?

 --John

 ___
 jQuery mailing list
 discuss@jquery.com
 http://jquery.com/discuss/


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Jörn Zaefferer
John Resig schrieb:
 $.fn.checked = function(b) {
 if ( b )
 this.attr( checked, checked );
 else
 this.removeAttr( checked );
 };

 What do you think?
   
How about this:
$.fn.checked = function(b) {
if( typeof b == 'boolean') {
if(b) {
  this.attr('checked, 'checked');
} else {
  this.removeAttr('checked');
} else {
   return this.attr('checked');
}
}

Could then be categorized within DOM/Attributes, as it aligns nicely 
with the rest of the set/get methods.

-- Jörn

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Klaus Hartl


John Resig schrieb:
 Hey everyone -
 
 I stumbled across a point of optimization today, all of the following
 attributes (and probably more - let me know) can only have a single
 value.
 
 checked=checked
 multiple=multiple
 disabled=disabled
 readonly=readonly
 disabled=disabled
 selected=selected
 
 I'd like to add in methods like:
 .checked( true | false )
 
 Which does this in the background (but for each attribute):
 
 $.fn.checked = function(b) {
 if ( b )
 this.attr( checked, checked );
 else
 this.removeAttr( checked );
 };
 
 What do you think?


John, these are great additions, but please be advised that in some 
browsers this.attr('checked', 'checked') won't work as expected. I 
suggest that way (unless the fix is already done in attr which I don't 
quite remember right now):

if ( b )
 this.checked = true;
else
 this.checked = false;

I think if you want to completely remove the attribute you can use 
removeAttr but that has to be distinguished from setting the value to false.


-- Klaus

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread John Resig
Or even more simply:

$.fn.checked = function(b) {
return b == undefined || b ?
this.attr('checked', b) :
this.removeAttr('checked');
};

This would handle the return statements nicely, along with non-boolean
values (in case someone  still wants to pass in the word checked,
for example).

--John

 $.fn.checked = function(b) {
 if( typeof b == 'boolean') {
 if(b) {
   this.attr('checked, 'checked');
 } else {
   this.removeAttr('checked');
 } else {
return this.attr('checked');
 }
 }

 Could then be categorized within DOM/Attributes, as it aligns nicely
 with the rest of the set/get methods.

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread John Resig
 John, these are great additions, but please be advised that in some
 browsers this.attr('checked', 'checked') won't work as expected.

Do you know which browsers those are offhand? It'll help me to test
the methods, when I make them

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Jörn Zaefferer
John Resig schrieb:
 Or even more simply:

 $.fn.checked = function(b) {
 return b == undefined || b ?
 this.attr('checked', b) :
 this.removeAttr('checked');
 };
   
Ugh. Calling this.attr('checked', b) in the case of b == undefined just 
returns the attribute, right? Creepy.
I like it.

-- Jörn

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread leandro nascimento camarg

What about you doing something like this:
$.fn.checked = function(b) {
b  this.attr('checked', 'checked') || this.removeAttr('checked');
}

but doing this we counting on both *attr* and *removeAttr* methods return
*true* on success (at least *attr* method).


John Resig wrote:
 
 $.fn.checked = function(b) {
 if ( b )
 this.attr( checked, checked );
 else
 this.removeAttr( checked );
 };
 
 What do you think?
 
 --John
 

-- 
View this message in context: 
http://www.nabble.com/Single-Value-Attributes-tf2373768.html#a6618527
Sent from the JQuery mailing list archive at Nabble.com.


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread leandro nascimento camarg

I strongly suggest you to compare if a variable is equal to undefined using
the typeof unary operator, because if you, someday, dicide to get these
things work on IE 5, you can not compare to the undefined constant (?),
after all, it doesn't exist on IE 5.0. Compare like this one:

if(b || typeof b == 'undefined') { ...



John Resig wrote:
 
 Sure, makes sense to me:
 
 $.fn.checked = function(b) {
if ( b || b == undefined )
this.attr( checked, checked );
else
this.removeAttr( checked );
 };
 
 --John
 

-- 
View this message in context: 
http://www.nabble.com/Single-Value-Attributes-tf2373768.html#a6618812
Sent from the JQuery mailing list archive at Nabble.com.


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Jörn Zaefferer
leandro nascimento camarg schrieb:
 I strongly suggest you to compare if a variable is equal to undefined using
 the typeof unary operator, because if you, someday, dicide to get these
 things work on IE 5, you can not compare to the undefined constant (?),
 after all, it doesn't exist on IE 5.0. Compare like this one:

 if(b || typeof b == 'undefined') { ...
   
Well, there is this fix:
window.undefined = window.undefined;

So far everything seems to work in IE5.5. Any IE versions lower than 
that won't be supported anyway, there are too many limitations on 
calling functions with a certain context, if I remember that right. In 
case you are interested, in this closed bug report is most of the stuff 
documented: http://jquery.com/dev/bugs/bug/96/

-- Jörn

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Klaus Hartl

John Resig schrieb:
 John, these are great additions, but please be advised that in some
 browsers this.attr('checked', 'checked') won't work as expected.
 
 Do you know which browsers those are offhand? It'll help me to test
 the methods, when I make them

I think there are overall inconstencies. I put up a test page:
http://stilbuero.de/demo/jquery/checkbox.html

There's no checked attribute in the HTML source. In Firefox if you set 
the checked attribute via attr the checkbox gets checked alright but 
removeAttr('checked') does not remove the checked state.

I found in general that it's better for all those boolean attributes to 
set/unset the boolean value.


HTH, Klaus

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Michael Geary
  $.fn.checked = function(b) {
 if ( b || b == undefined )
 this.attr( checked, checked );
 else
 this.removeAttr( checked );
  };

 I strongly suggest you to compare if a variable is equal to 
 undefined using the typeof unary operator, because if you, 
 someday, dicide to get these things work on IE 5, you can not 
 compare to the undefined constant (?), after all, it doesn't 
 exist on IE 5.0. Compare like this one:
 
 if(b || typeof b == 'undefined') { ...

No need to go to the extra work. jquery.js begins with this:

window.undefined = window.undefined;

So undefined exists in every browser. This is a handy line of code to put
in any JavaScript - it's completely compatible with both old and new
browsers.

-Mike


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Dave Methvin
 
 John, these are great additions, but please be advised that in some 
 browsers this.attr('checked', 'checked') won't work as expected.
 
 Do you know which browsers those are offhand? It'll help me to test 
 the methods, when I make them

 I think there are overall inconstencies. I put up a test page:
 http://stilbuero.de/demo/jquery/checkbox.html

Also notice that .innerHTML (and thus jQuery's .html() method) doesn't
always return the live checked state. In IE it tracks any dynamic changes to
the script, but in Firefox it only reflects the initial state from the
source. You can see the form text by putting this into the URL: 

javascript:alert(document.getElementsByTagName(form)[0].innerHTML)


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread John Resig
Ah, this isn't a browser bug, but something of a jQuery one.
removeAttr does obj.removeAttribute(checked) under the hood, which
is not sufficient to remove these built-in properties. It would have
to do obj.checked = undefined; or some such under the hood.

--John

On 10/3/06, Klaus Hartl [EMAIL PROTECTED] wrote:

 John Resig schrieb:
  John, these are great additions, but please be advised that in some
  browsers this.attr('checked', 'checked') won't work as expected.
 
  Do you know which browsers those are offhand? It'll help me to test
  the methods, when I make them

 I think there are overall inconstencies. I put up a test page:
 http://stilbuero.de/demo/jquery/checkbox.html

 There's no checked attribute in the HTML source. In Firefox if you set
 the checked attribute via attr the checkbox gets checked alright but
 removeAttr('checked') does not remove the checked state.

 I found in general that it's better for all those boolean attributes to
 set/unset the boolean value.


 HTH, Klaus

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread John Resig
 Also notice that .innerHTML (and thus jQuery's .html() method) doesn't
 always return the live checked state. In IE it tracks any dynamic changes to
 the script, but in Firefox it only reflects the initial state from the
 source. You can see the form text by putting this into the URL:

 javascript:alert(document.getElementsByTagName(form)[0].innerHTML)

That's just weird. I've never seen a bug like that before. Although,
even Firebug doesn't display the checked state so it must be a global
thing.

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-03 Thread Dave Benjamin
On Tue, 3 Oct 2006, Michael Geary wrote:

 No need to go to the extra work. jquery.js begins with this:

 window.undefined = window.undefined;

 So undefined exists in every browser. This is a handy line of code to put
 in any JavaScript - it's completely compatible with both old and new
 browsers.

Hah! JavaScript is SO WEIRD!

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


[jQuery] Single Value Attributes

2006-10-02 Thread John Resig
Hey everyone -

I stumbled across a point of optimization today, all of the following
attributes (and probably more - let me know) can only have a single
value.

checked=checked
multiple=multiple
disabled=disabled
readonly=readonly
disabled=disabled
selected=selected

I'd like to add in methods like:
.checked( true | false )

Which does this in the background (but for each attribute):

$.fn.checked = function(b) {
if ( b )
this.attr( checked, checked );
else
this.removeAttr( checked );
};

What do you think?

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-02 Thread Brian
What happens if you call .checked() with no args?  Wouldn't that
automatically read as false, leading to $( '#foo' ).checked() unchecking
the checkbox?

If called without any arguments, it should behave as if called with true
as an argument.

- Brian


 Hey everyone -

 I stumbled across a point of optimization today, all of the following
 attributes (and probably more - let me know) can only have a single
 value.

 checked=checked
 multiple=multiple
 disabled=disabled
 readonly=readonly
 disabled=disabled
 selected=selected

 I'd like to add in methods like:
 .checked( true | false )

 Which does this in the background (but for each attribute):

 $.fn.checked = function(b) {
 if ( b )
 this.attr( checked, checked );
 else
 this.removeAttr( checked );
 };

 What do you think?

 --John


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-02 Thread Matt Grimm
I'm all for that. I've always thought it awkward of XHTML to have the
value of those attributes be the same as their name. Naturally, you may
want to support the existing syntax as well for backwards compatibility.

m. 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of John Resig
Sent: Monday, October 02, 2006 6:41 PM
To: jQuery Discussion.
Subject: [jQuery] Single Value Attributes

Hey everyone -

I stumbled across a point of optimization today, all of the following
attributes (and probably more - let me know) can only have a single
value.

checked=checked
multiple=multiple
disabled=disabled
readonly=readonly
disabled=disabled
selected=selected

I'd like to add in methods like:
.checked( true | false )

Which does this in the background (but for each attribute):

$.fn.checked = function(b) {
if ( b )
this.attr( checked, checked );
else
this.removeAttr( checked );
};

What do you think?

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-02 Thread John Resig
 What happens if you call .checked() with no args?  Wouldn't that
 automatically read as false, leading to $( '#foo' ).checked() unchecking
 the checkbox?

 If called without any arguments, it should behave as if called with true
 as an argument.

Sure, makes sense to me:

$.fn.checked = function(b) {
   if ( b || b == undefined )
   this.attr( checked, checked );
   else
   this.removeAttr( checked );
};

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Single Value Attributes

2006-10-02 Thread John Resig
 I'm all for that. I've always thought it awkward of XHTML to have the
 value of those attributes be the same as their name. Naturally, you may
 want to support the existing syntax as well for backwards compatibility.

.attr() isn't going anywhere, nor is .removeAttr(). I assume that's
what you mean by existing syntax. Yeah, this would just be a simple
addition.

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/