DirtyRedz
DirtyRedz

Reputation: 566

javascript If Else block is jumping to Else, even when the Conditional is marking true

I am attempting to perform an operation based on a true or false value from a checkbox. Using an If:Else statment I test if the checkbox value is "true", onfurtunetly even if the checkbox value marks "true" its going into the Else section of my statment.

Can anyone please explain to me the reason for this?

        var CheckedBoxs = $(stepTitle + " input:checkbox");
        for (var val in CheckedBoxs) {
            if (val.toString() === "length") {
                break;
            } //End IF
            if (CheckedBoxs[val].value === "true") {
                OneUp = parseInt(val) + 1;
                var PriceTag = "  -$";
                PriceTag += HiddenInput[OneUp].name.substring(HiddenInput[OneUp].name.indexOf(":") + 1, HiddenInput[OneUp].name.length);
                Lbls[val].innerHTML = Lbls[val].innerHTML + '<span class="PriceChange">' + PriceTag + "</span>";
            } else {
                OneUp = parseInt(val) + 1;
                var PriceTag = "  +$";
                PriceTag += HiddenInput[OneUp].name.substring(HiddenInput[OneUp].name.indexOf(":") + 1, HiddenInput[OneUp].name.length);
                Lbls[val].innerHTML = Lbls[val].innerHTML + '<span class="PriceChange">' + PriceTag + "</span>";
            }//End If
        }//End For

I have seen many question related to If:Else statments, but was unable to find one that invloved a checkbox, apologies if this is a repeat.

Upvotes: 0

Views: 80

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120258

Two things

First

for (var val in CheckedBoxs) {

that is not how to loop over an array; only use that loop over properties of an object literal.

Second

if (CheckedBoxs[val].value === "true") {

should be

if (CheckedBoxs[val].checked) {

Edit, fiddle

Upvotes: 3

Related Questions