Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#8500 closed bug (fixed)

radios and checkboxes revert to default (HTML) state when wrapped in IE

Reported by: gnarf Owned by: gnarf
Priority: low Milestone: 1.6
Component: manipulation Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:

Description

http://jsfiddle.net/eGgvw/6/

calling .wrap() on something that contains a radio or checkbox will cause it to revert to the state in the HTML that created it.

Change History (17)

comment:1 Changed 11 years ago by gnarf

I apologize for duping http://bugs.jquery.com/ticket/769 but to hopefully redeem myself I will correct with a punchable plugin for those that need this in IE6: http://jsfiddle.net/eGgvw/13/

(function($) {
    var _append = $.fn.append;

    function getChecked( el ) {
        if ( el && el.jquery ) {
            return el.length == 1 && el[0].nodeName == "INPUT" && el[0].checked ? 
                el : 
                el.find('input:checked');
        }
        return $();
    }

    $.fn.append = function( arg ) {
        var chk = getChecked( arg ),
            ret = _append.apply( this, arguments );
        getChecked( arg ).removeAttr( 'checked' );
        chk.attr( 'checked', 'checked' );
        return ret;
    };
})(jQuery);
Last edited 11 years ago by gnarf (previous) (diff)

comment:2 Changed 11 years ago by gnarf

So far the closest to a feature detect I've gotten is this:

var div = document.createElement(div),
	input;

div.innerHTML = "<input type='checkbox' />";
input = div.getElementsByTagName( "input" )[ 0 ];

// IE6 actually sets checked to true here, and if it doesn't work, the
// appendChild doesn't clear the checked state - it should be fairly safe
input.click();
jQuery.support.noAppendChecked = input.checked && !div.appendChild(input).checked;
Version 0, edited 11 years ago by gnarf (next)

comment:3 Changed 11 years ago by gnarf

http://jsperf.com/append-checked -- Might not be the right way to test the effects, but they don't seem that drastically bad here...

comment:4 Changed 11 years ago by scottgonzalez

comment:5 Changed 11 years ago by dmethvin

See also #1736, where someone was asking for the ability to preserve arbitrary expando properties. I think it's better to just deal with the critical ones like checked as you've done here.

But... doesn't this problem iron itself out if the attr rewrite sets the checked attribute when someone uses $(":checkbox").attr("checked", true); ? That way when the element is cloned and/or serialized it will reflect the current check state.

Of course, if someone uses $(":text").attr("value", "abc"); that would also change the serialized state of the element. I think that $(":text").val("abc"); should only set the dynamic state (value property) and leave the value attribute unmolested.

comment:6 Changed 11 years ago by dmethvin

Nope, setting the checked attribute doesn't seem to be enough for IE6:

http://jsfiddle.net/AhhJh/

comment:7 in reply to:  5 Changed 11 years ago by gnarf

Replying to dmethvin:

See also #1736, where someone was asking for the ability to preserve arbitrary expando properties. I think it's better to just deal with the critical ones like checked as you've done here.

Agreed

But... doesn't this problem iron itself out if the attr rewrite sets the checked attribute when someone uses $(":checkbox").attr("checked", true); ? That way when the element is cloned and/or serialized it will reflect the current check state.

Yeah, but thats not going to catch the actual user clicking on a radio to change it, unless you are suggesting they add a handler to every checkbox to set the attr to what it is, which could be a valid solution too...

Of course, if someone uses $(":text").attr("value", "abc"); that would also change the serialized state of the element. I think that $(":text").val("abc"); should only set the dynamic state (value property) and leave the value attribute unmolested.

comment:8 Changed 11 years ago by Rick Waldron

Component: unfiledattributes
Owner: set to gnarf
Priority: undecidedlow
Status: newassigned

comment:9 Changed 11 years ago by timmywil

The test case seems to work fine now: http://jsfiddle.net/timmywil/eGgvw/21/

comment:10 Changed 11 years ago by timmywil

#8060 is a duplicate of this ticket.

comment:11 Changed 11 years ago by timmywil

Component: attributesmanipulation
Version: 1.5.11.5.2

I didn't see the real problem before. This is a manip bug (IE6/7 can't handle keeping checked on appendChild).

Gnarf's duck punch above works to fix #8060. Patch incoming. http://jsfiddle.net/timmywil/MwtCW/8/

comment:12 Changed 11 years ago by gnarf

Last edited 11 years ago by gnarf (previous) (diff)

comment:13 Changed 11 years ago by timmywil

Resolution: duplicate
Status: assignedclosed

comment:14 Changed 11 years ago by timmywil

Duplicate of #8060.

comment:15 Changed 11 years ago by timmywil

Resolution: duplicatefixed

Landing pull request 332. Appending disconnected radio or checkbox inputs and keeping checked setting Fixes #8060, #8500.

More Details:

comment:16 Changed 11 years ago by john

Milestone: 1.next1.6

comment:17 Changed 11 years ago by timmywil

#9470 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.