I am trying trying to write a string of multiple values depending on whether specific checkboxes are checked. i.e.
$('#submit').click(function() {
if ($('#box1').attr('checked')) {
$('#MyInput').val('box1 ');
alert('1');
}
if ($('#box2').attr('checked')) {
$('#MyInput').val('box2 ');
alert('2');
}
});
The return I am looking for in this case would be ‘1 2’, however only the second value is written in the input field.
The alerts tell me that both events in the argument are firing. Is there another method for appending values?
The
.val()does not append.. it sets the value to whatever you pass, overwriting the previous value.And if you have multiple checkboxes that you want all of their values added to the myInput element if they are checked you can add a class to all of them and just do
demo http://jsfiddle.net/SQcGq/