Bug Tracker

Ticket #8500 (closed bug: fixed)

Opened 3 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

comment:1 Changed 3 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 3 years ago by gnarf (previous) (diff)

comment:2 Changed 3 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();
// interestingly - if you set input.checked = true it will work - odd eh?
// and maybe we should call this "appendResetsChecked"
jQuery.support.noAppendChecked = input.checked && !div.appendChild(input).checked;
Last edited 3 years ago by gnarf (previous) (diff)

comment:3 Changed 3 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 3 years ago by scott.gonzalez

comment:5 follow-up: ↓ 7 Changed 3 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 3 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 3 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 3 years ago by rwaldron

  • Owner set to gnarf
  • Priority changed from undecided to low
  • Status changed from new to assigned
  • Component changed from unfiled to attributes

comment:9 Changed 3 years ago by timmywil

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

comment:10 Changed 3 years ago by timmywil

#8060 is a duplicate of this ticket.

comment:11 Changed 3 years ago by timmywil

  • Version changed from 1.5.1 to 1.5.2
  • Component changed from attributes to manipulation

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 3 years ago by gnarf

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

comment:13 Changed 3 years ago by timmywil

  • Status changed from assigned to closed
  • Resolution set to duplicate

comment:14 Changed 3 years ago by timmywil

Duplicate of #8060.

comment:15 Changed 3 years ago by timmywil

  • Resolution changed from duplicate to fixed

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

More Details:

comment:16 Changed 3 years ago by john

  • Milestone changed from 1.next to 1.6

comment:17 Changed 3 years ago by timmywil

#9470 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.