Skip to content

Commit

Permalink
Added default value for checkboxes/radios
Browse files Browse the repository at this point in the history
  • Loading branch information
twolfson committed Feb 1, 2016
1 parent c3ec1cd commit 046071a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ var getAttr = function(elem, name) {
if (elem.name === 'option' && name === 'value') {
return $.text(elem.children);
}

// Mimic DOM with default value for radios/checkboxes
if (elem.name === 'input' &&
(elem.attribs.type === 'radio' || elem.attribs.type === 'checkbox') &&
name === 'value') {
return 'on';
}
};

var setAttr = function(el, name, value) {
Expand Down
8 changes: 8 additions & 0 deletions test/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,18 @@ describe('$(...)', function() {
var val = $('input[name="checkbox_off"]').val();
expect(val).to.equal('off');
});
it('.val(): on valueless checkbox should get value', function() {
var val = $('input[name="checkbox_valueless"]').val();
expect(val).to.equal('on');
});
it('.val(): on radio should get value', function() {
var val = $('input[type="radio"]').val();
expect(val).to.equal('off');
});
it('.val(): on valueless radio should get value', function() {
var val = $('input[name="radio_valueless"]').val();
expect(val).to.equal('on');
});
it('.val(): on multiple select should get an array of values', function() {
var val = $('select#multi').val();
expect(val).to.eql(['2', '3']);
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ exports.inputs = [
'<select id="one-nested"><option>Option not selected</option><option selected>Option <span>selected</span></option></select>',
'<input type="text" value="input_text" />',
'<input type="checkbox" name="checkbox_off" value="off" /><input type="checkbox" name="checkbox_on" value="on" checked />',
'<input type="checkbox" name="checkbox_valueless" />',
'<input type="radio" value="off" name="radio" /><input type="radio" name="radio" value="on" checked />',
'<input type="radio" value="off" name="radio[brackets]" /><input type="radio" name="radio[brackets]" value="on" checked />',
'<input type="radio" name="radio_valueless" />',
'<select id="multi" multiple><option value="1">1</option><option value="2" selected>2</option><option value="3" selected>3</option><option value="4">4</option></select>',
'<select id="multi-valueless" multiple><option>1</option><option selected>2</option><option selected>3</option><option>4</option></select>'
].join('');
Expand Down

0 comments on commit 046071a

Please sign in to comment.