0

I have some checkboxes which feature days of the week. I simply want to loop through, and display all those selected within an append.

HTML

<div class="col-lg-8">
 <input type="checkbox" value="Mon" class="dayname" name="days[]"> M
 <input type="checkbox" value="Tues" class="dayname" name="days[]"> T
 <input type="checkbox" value="Wed" class="dayname" name="days[]">W
 <input type="checkbox" value="Thurs" class="dayname" name="days[]">T
 <input type="checkbox" value="Fri" class="dayname" name="days[]">F
 <input type="checkbox" value="Sat" class="dayname" name="days[]">S
 <input type="checkbox" value="Sun" class="dayname" name="days[]">S
</div>

Javascript

$(".dayname").click(function() {
   alert( $(this).attr("name") );
   $('.dayname').val();
});

4 Answers 4

2

Try this: JSFIDDLE

$(".dayname").click(function() {
    alert( $(this).attr("name") );

    $('#log').html(''); // Clears the log div

    $('.dayname:checked').each(function(e){
        $('#log').append('<p>'+$(this).val()+'</p>');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use .map().get():

$(".dayname").change(function() {
   var days = $('.dayname:checked').map(function(){
                 return this.value;
              }).get();
   console.log(days); // prints the checked checkboxes values
});

Comments

-1
var checkedDays=[];
$(".dayname").click(function() {
   //alert( $(this).attr("name") );
   $('.dayname:checked').each(function(e){
      //alert($(this).val());

   });
    checkedDays.push($(this).val());
   alert(checkedDays);
});

Comments

-1
$(".dayname").click(function() {
  alert( $(this).attr("value") );
});

Or

$(".dayname").change(function() {
if($(this).is(":checked")) {
    alert($(this).attr('value'));
}
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.