0

I have a string with values and i want to check the checkboxes which have these values in the string:

var daysofweek = '1,3,4,';

<input class="edit-day" type="checkbox" value="1"><label class="form-check-label">Monday</label>
<input class="edit-day" type="checkbox" value="2"><label class="form-check-label">Tuesday</label>
<input class="edit-day" type="checkbox" value="3"><label class="form-check-label">Wednesday</label>
<input class="edit-day" type="checkbox" value="4"><label class="form-check-label">Thursday</label>
<input class="edit-day" type="checkbox" value="5"><label class="form-check-label">Friday</label>
<input class="edit-day" type="checkbox" value="6"><label class="form-check-label">Saturday</label>
<input class="edit-day" type="checkbox" value="0"><label class="form-check-label">Sunday</label> // sunday has 0

In this case, Monday, Wednesday and Thursday should be checked

How can i do that?

I tried something with code below but that does not work:

$.each(daysofweek, function(i, val){
    $('.edit-day').prop('checked', true);
});

1 Answer 1

2

You either have to split the string, or convert it to an array like the example below:

var daysofweek = [1, 3, 4, ];

$.each(daysofweek, function(i, val) {
  $('.edit-day[value=' + val + ']').prop('checked', true);
});

If daysofweek have to be a string then use daysofweek.split(',')

Demo

var daysofweek = [1, 3, 4, ];

$.each(daysofweek, function(i, val) {
  $('.edit-day[value=' + val + ']').prop('checked', true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="edit-day" type="checkbox" value="1"><label class="form-check-label">Monday</label>
<input class="edit-day" type="checkbox" value="2"><label class="form-check-label">Tuesday</label>
<input class="edit-day" type="checkbox" value="3"><label class="form-check-label">Wednesday</label>
<input class="edit-day" type="checkbox" value="4"><label class="form-check-label">Thursday</label>
<input class="edit-day" type="checkbox" value="5"><label class="form-check-label">Friday</label>
<input class="edit-day" type="checkbox" value="6"><label class="form-check-label">Saturday</label>
<input class="edit-day" type="checkbox" value="0"><label class="form-check-label">Sunday</label>

Other example

var daysofweek = [1, 3, 4, ];

$('.edit-day').prop('checked', function() {
  return $.inArray(+$(this).val(), daysofweek) != -1
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="edit-day" type="checkbox" value="1"><label class="form-check-label">Monday</label>
<input class="edit-day" type="checkbox" value="2"><label class="form-check-label">Tuesday</label>
<input class="edit-day" type="checkbox" value="3"><label class="form-check-label">Wednesday</label>
<input class="edit-day" type="checkbox" value="4"><label class="form-check-label">Thursday</label>
<input class="edit-day" type="checkbox" value="5"><label class="form-check-label">Friday</label>
<input class="edit-day" type="checkbox" value="6"><label class="form-check-label">Saturday</label>
<input class="edit-day" type="checkbox" value="0"><label class="form-check-label">Sunday</label>

Sign up to request clarification or add additional context in comments.

2 Comments

Super! thnx for this solution
@john No problem, happy to help

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.