Skip to content
Browse files

Really basic support for a JQuery-like val() method.

  • Loading branch information...
1 parent d4d51fe commit fe781ad6f4c3d1fdc26e9c985458d1f649119b88 Simon Russell committed Jan 7, 2011
Showing with 22 additions and 1 deletion.
  1. +8 −1 src/zepto.js
  2. +14 −0 test/zepto.html
View
9 src/zepto.js
@@ -114,13 +114,20 @@ var Zepto = (function() {
},
attr: function(name, value){
return (typeof name == 'string' && value === undefined) ?
- (this.length > 0 && this.dom[0].nodeName === 'INPUT' && this.dom[0].type === 'text' && name === 'value') ? (this.dom[0].value) :
+ (this.length > 0 && this.dom[0].nodeName === 'INPUT' && this.dom[0].type === 'text' && name === 'value') ? (this.val()) :
(this.length > 0 ? this.dom[0].getAttribute(name) || undefined : null) :
this.each(function(){
if (typeof name == 'object') for (key in name) this.setAttribute(key, name[key])
else this.setAttribute(name, value);
});
},
+ val: function(value){
+ return (value === undefined) ?
+ (this.length > 0 ? this.dom[0].value : null) :
+ this.each(function(){
+ this.value = value;
+ });
+ },
offset: function(){
var obj = this.dom[0].getBoundingClientRect();
return {
View
14 test/zepto.html
@@ -679,6 +679,20 @@
},
+ testVal: function(t) {
+ var els = $('#attr_with_text_input');
+
+ els.get(0).value = "Hello World";
+
+ t.assertEqual(els.get(0).value, els.val());
+ t.assertEqual('Hello World', els.val());
+
+ els.val('');
+ t.assertEqual(els.get(0).value, els.val());
+ t.assertEqual('', els.val());
+
+ },
+
testChaining: function(t){
t.assert(document.getElementById('nay').innerHTML == "nay");
$('span.nay').css('color', 'red').html('test');

0 comments on commit fe781ad

Please sign in to comment.
Something went wrong with that request. Please try again.