0

I have multiple checkboxes added with a button. I want to assign a value of 150 for each checkbox that is checked. Maybe my logic is wrong, but i cant get it working. Ideas?

function getValues() {

    var cost = 0;
    var isChecked = $('.isLab').prop('checked');

    $('.isLab').each(function () {
        if (isChecked == true) {
            cost = 150
        }
    });
    alert(cost);
}

2 Answers 2

2

$('button').click(getValues);

function getValues() {
    alert( $('.isLab:checked').length * 150 );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="checkbox" class="isLab"/> Item 1<br/>
<input type="checkbox" class="isLab"/> Item 2<br/>
<input type="checkbox" class="isLab"/> Item 3<br/>
<button>Get values</button>

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

1 Comment

@ehuizar We tend to overthink sometimes ;)
0

Comments inline...

function getValues() {

    var cost = 0;

    $('.isLab').each(function (index, element) {
        if (element.checked == true) { // reference current checkbox
            cost += 150; // ADD to it!
        }
    });
    alert(cost);
}

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.