diff options
author | John Resig <jeresig@gmail.com> | 2007-09-04 04:17:14 +0000 |
---|---|---|
committer | John Resig <jeresig@gmail.com> | 2007-09-04 04:17:14 +0000 |
commit | 7d02f06e036f6a42ccd2c276e9f00a7cd35dc74a (patch) | |
tree | b72feebffc6d35f15177914562afb507f311f2d4 | |
parent | 53dc6afc310aa0e5df094304996ef605d4dbbd58 (diff) | |
download | jquery-7d02f06e036f6a42ccd2c276e9f00a7cd35dc74a.tar.gz jquery-7d02f06e036f6a42ccd2c276e9f00a7cd35dc74a.zip |
Made it so that you can't change the type of an input element, having it throw an exception instead (except for input elements that haven't yet been injected into the DOM). (Bug #1536)
-rw-r--r-- | src/jquery/coreTest.js | 24 | ||||
-rw-r--r-- | src/jquery/jquery.js | 8 |
2 files changed, 30 insertions, 2 deletions
diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js index bb51acbea..794bb5a29 100644 --- a/src/jquery/coreTest.js +++ b/src/jquery/coreTest.js @@ -232,7 +232,7 @@ test("attr(Hash)", function() { }); test("attr(String, Object)", function() { - expect(8); + expect(12); var div = $("div"); div.attr("foo", "bar"); var pass = true; @@ -255,6 +255,28 @@ test("attr(String, Object)", function() { ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' ); $("#name").attr('maxlength', '5'); ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' ); + + reset(); + + var type = $("#check2").attr('type'); + var thrown = false; + try { + $("#check2").attr('type','hidden'); + } catch(e) { + thrown = true; + } + ok( thrown, "Exception thrown when trying to change type property" ); + equals( type, $("#check2").attr('type'), "Verify that you can't change the type of an input element" ); + + var check = document.createElement("input"); + var thrown = true; + try { + $(check).attr('type','checkbox'); + } catch(e) { + thrown = false; + } + ok( thrown, "Exception thrown when trying to change type property" ); + equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" ); }); test("attr(String, Object) - Loaded via XML document", function() { diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 4f3e03e2d..09e714cc5 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -728,9 +728,15 @@ jQuery.extend({ // IE elem.getAttribute passes even for style else if ( elem.tagName ) { - if ( value != undefined ) elem.setAttribute( name, value ); + if ( value != undefined ) { + if ( name == "type" && jQuery.nodeName(elem,"input") && elem.parentNode ) + throw "type property can't be changed"; + elem.setAttribute( name, value ); + } + if ( jQuery.browser.msie && /href|src/.test(name) && !jQuery.isXMLDoc(elem) ) return elem.getAttribute( name, 2 ); + return elem.getAttribute( name ); // elem is actually elem.style ... set the style |