From 67d4aebda7da4eaeff8cd4c2c0a66718504f339e Mon Sep 17 00:00:00 2001 From: Timmy Willison Date: Fri, 15 Jan 2016 16:48:52 -0500 Subject: [PATCH] Attributes: fix setting selected on an option in IE<=11 Fixes gh-2732 Close gh-2840 --- src/attributes/prop.js | 16 ++++++++++++++++ test/unit/attributes.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/attributes/prop.js b/src/attributes/prop.js index da7bc1e86..15128b8ce 100644 --- a/src/attributes/prop.js +++ b/src/attributes/prop.js @@ -79,6 +79,12 @@ jQuery.extend( { } } ); +// Support: IE <=11 only +// Accessing the selectedIndex property +// forces the browser to respect setting selected +// on the option +// The getter ensures a default option is selected +// when in an optgroup if ( !support.optSelected ) { jQuery.propHooks.selected = { get: function( elem ) { @@ -87,6 +93,16 @@ if ( !support.optSelected ) { parent.parentNode.selectedIndex; } return null; + }, + set: function( elem ) { + var parent = elem.parentNode; + if ( parent ) { + parent.selectedIndex; + + if ( parent && parent.parentNode ) { + parent.parentNode.selectedIndex; + } + } } }; } diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 8b31b72a6..e52e85b49 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -785,6 +785,37 @@ QUnit.test( "prop('tabindex', value)", function( assert ) { assert.equal( clone[ 0 ].getAttribute( "tabindex" ), "1", "set tabindex on cloned element" ); } ); +QUnit.test( "option.prop('selected', true) affects select.selectedIndex (gh-2732)", function( assert ) { + assert.expect( 2 ); + + function addOptions( $elem ) { + return $elem.append( + jQuery( "