Condition javascript — how to solve?

Hello!!

Help, who can(

Task:

there is a piece of code:
var kap=$(".kap input:checked").attr("data-id");

If chekbox is checked, we get the data-id as finished code, so that if the checkbox is unchecked, too, was the answer (the same data-id for example)

I know that it is in the spirit if else...but I can not afford(
June 10th 19 at 14:31
3 answers
June 10th 19 at 14:33
var checkbox = $('.kap input[type="checkbox"]');
var kap = checkbox.is(':checked') ? checkbox.data('id') : 'No';

The second line is shorter if-else:
if (checkbox.is(':checked')) {
 var kap = checkbox.data('id');
} else {
 var kap = 'No';
};
June 10th 19 at 14:35
Not that I need something in the spirit

var kap=$(".kap input:checked").attr("data-id");
else
var kap=$(".kap input:not-checked").attr("data-id");
- nikita.Stracke commented on June 10th 19 at 14:38
June 10th 19 at 14:37
Now this: If if is checked, then outputs the data from the data-id, and if not checked , then nothing is displayed.
I need if the option was not - then displayed the word "No"
var kap=$(".kap input:checked").attr("data-id");

Find more questions by tags JavaScript