jQuery: The Write Less, Do More JavaScript Library

.attr() brakes the chain if setting to value 'undefined'

View: New views
2 Messages — Rating Filter:   Alert me  

.attr() brakes the chain if setting to value 'undefined'

by Jonathan Sharp-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Given the following:

$.fn.foo = function() { alert('foo'); return this };

$( [] ).attr('id', bar).foo();

.foo() is never executed because bar was never initialized and is 'undefined'. The attr() method then thinks that it's getting instead of setting and returns undefined thus breaking the chain.

jQuery.fn.attr currently tests to see if a value needs to be set by the following case:
attr: function( elem, name, value ) {
    ...
    set = value !== undefined;

In this case value really was passed in but fails to be a valid value for setting.

Expected:
If an attribute is attempted to be set with an undefined value, either attempt to set the value to 'undefined' or at least return the jQuery object to continue chaining.

Core team, want me to open a bug?

Cheers,
- Jonathan

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: .attr() brakes the chain if setting to value 'undefined'

by James Padolsey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


This could be fixed by checking arguments.length.

---
set = arguments.length > 1;
---

On Oct 23, 5:12 pm, Jonathan Sharp <jdsh...@...> wrote:

> Given the following:
>
> $.fn.foo = function() { alert('foo'); return this };
>
> $( [] ).attr('id', bar).foo();
>
> .foo() is never executed because bar was never initialized and is
> 'undefined'. The attr() method then thinks that it's getting instead of
> setting and returns undefined thus breaking the chain.
>
> jQuery.fn.attr currently tests to see if a value needs to be set by the
> following case:
> attr: function( elem, name, value ) {
>     ...
>     set = value !== undefined;
>
> In this case value really was passed in but fails to be a valid value for
> setting.
>
> Expected:
> If an attribute is attempted to be set with an undefined value, either
> attempt to set the value to 'undefined' or at least return the jQuery object
> to continue chaining.
>
> Core team, want me to open a bug?
>
> Cheers,
> - Jonathan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---