Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patch for jQuery ticket #1954 using support.js
diff --git a/src/attributes.js b/src/attributes.js
index 5997210..297bcc2 100644
--- a/src/attributes.js
+++ b/src/attributes.js
@@ -31,7 +31,11 @@ jQuery.fn.extend({
return this.each(function(){
jQuery.attr( this, name, "" );
if ( this.nodeType === 1 ) {
- this.removeAttribute( name );
+ if ( !jQuery.support.buttonValue && name === "value" && jQuery.nodeName( this, "button" ) ) {
+ this.removeAttributeNode(this.getAttributeNode( name ));
+ } else {
+ this.removeAttribute( name );
+ }
}
});
},
@@ -211,6 +215,10 @@ jQuery.fn.extend({
return elem.getAttribute("value") === null ? "on" : elem.value;
}
+ if ( !jQuery.support.buttonValue && jQuery.nodeName( elem, "button" ) ) {
+ return elem.getAttributeNode( "value" ) ? elem.getAttributeNode( "value" ).nodeValue : "";
+ }
+
// Everything else, we just grab the value
return (elem.value || "").replace(rreturn, "");
@@ -257,6 +265,17 @@ jQuery.fn.extend({
this.selectedIndex = -1;
}
+ } else if ( !jQuery.support.buttonValue && jQuery.nodeName( this, "button" ) ) {
+ var attributeNode = this.getAttributeNode( "value" );
+
+ if (attributeNode) {
+ attributeNode.nodeValue = val;
+ } else {
+ attributeNode = document.createAttribute( "value" );
+ attributeNode.nodeValue = val;
+ this.setAttributeNode( attributeNode );
+ }
+
} else {
this.value = val;
}
@@ -323,7 +342,22 @@ jQuery.extend({
if ( value === null ) {
if ( elem.nodeType === 1 ) {
- elem.removeAttribute( name );
+ if ( !jQuery.support.buttonValue && name === "value" && jQuery.nodeName( elem, "button" ) ) {
+ elem.removeAttributeNode(elem.getAttributeNode( name ));
+ } else {
+ elem.removeAttribute( name );
+ }
+ }
+
+ } else if ( !jQuery.support.buttonValue && name === "value" && jQuery.nodeName( elem, "button" ) ) {
+ var attributeNode = elem.getAttributeNode( name );
+
+ if (attributeNode) {
+ attributeNode.nodeValue = value;
+ } else {
+ attributeNode = document.createAttribute("value");
+ attributeNode.nodeValue = value;
+ elem.setAttributeNode(attributeNode);
}
} else {
@@ -336,6 +370,10 @@ jQuery.extend({
return elem.getAttributeNode( name ).nodeValue;
}
+ if ( !jQuery.support.buttonValue && name === "value" && jQuery.nodeName( elem, "button" ) ) {
+ return elem.getAttributeNode( name ) ? elem.getAttributeNode( name ).nodeValue : "";
+ }
+
// elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
// http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
if ( name === "tabIndex" ) {
diff --git a/src/support.js b/src/support.js
index 7470b33..c99b786 100644
--- a/src/support.js
+++ b/src/support.js
@@ -7,7 +7,7 @@
var div = document.createElement("div");
div.style.display = "none";
- div.innerHTML = " <link/><table></table><a href='/a' style='color:red;float:left;opacity:.55;'>a</a><input type='checkbox'/>";
+ div.innerHTML = " <link/><table></table><a href='/a' style='color:red;float:left;opacity:.55;'>a</a><input type='checkbox'/><button>a</button>";
var all = div.getElementsByTagName("*"),
a = div.getElementsByTagName("a")[0],
@@ -54,6 +54,10 @@
// (WebKit defaults to "" instead)
checkOn: input.value === "on",
+ // Make sure that button values are reported correctly
+ // (IE7 returns innerHTML instead)
+ buttonValue: div.getElementsByTagName("button")[0].value === "",
+
// Make sure that a selected-by-default option has a working selected property.
// (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
optSelected: opt.selected,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.