Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3800d17

Browse files
committed
feat(jqLite): add prop() support
since jQuery 1.6.4 prop() became very important because attr() does't have access to certain properties any more (e.g. className), so I'm adding it to jqLite as well so that jqLite preserves the feature-set it had before the jQuery upgrade.
1 parent 009059d commit 3800d17

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/jqLite.js

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* - [eq()](http://api.jquery.com/eq/)
4141
* - [hasClass()](http://api.jquery.com/hasClass/)
4242
* - [parent()](http://api.jquery.com/parent/)
43+
* - [prop()](http://api.jquery.com/prop/)
4344
* - [remove()](http://api.jquery.com/remove/)
4445
* - [removeAttr()](http://api.jquery.com/removeAttr/)
4546
* - [removeClass()](http://api.jquery.com/removeClass/)
@@ -287,6 +288,14 @@ forEach({
287288
}
288289
},
289290

291+
prop: function(element, name, value) {
292+
if (isDefined(value)) {
293+
element[name] = value;
294+
} else {
295+
return element[name];
296+
}
297+
},
298+
290299
text: extend((msie < 9)
291300
? function(element, value) {
292301
// NodeType == 3 is text node

test/jqLiteSpec.js

+32
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ describe('jqLite', function(){
171171
});
172172

173173

174+
describe('prop', function() {
175+
it('should read element property', function() {
176+
var elm = jqLite('<div class="foo">a</div>');
177+
expect(elm.prop('className')).toBe('foo');
178+
});
179+
180+
it('should set element property to a value', function() {
181+
var elm = jqLite('<div class="foo">a</div>');
182+
elm.prop('className', 'bar');
183+
expect(elm[0].className).toBe('bar');
184+
expect(elm.prop('className')).toBe('bar');
185+
});
186+
187+
it('should set boolean element property', function() {
188+
var elm = jqLite('<input type="checkbox">');
189+
expect(elm.prop('checked')).toBe(false);
190+
191+
elm.prop('checked', true);
192+
expect(elm.prop('checked')).toBe(true);
193+
194+
elm.prop('checked', '');
195+
expect(elm.prop('checked')).toBe(false);
196+
197+
elm.prop('checked', 'lala');
198+
expect(elm.prop('checked')).toBe(true);
199+
200+
elm.prop('checked', null);
201+
expect(elm.prop('checked')).toBe(false);
202+
});
203+
});
204+
205+
174206
describe('class', function(){
175207

176208
describe('hasClass', function(){

0 commit comments

Comments
 (0)