Checkboxes show undefined value

For some reason this isn’t working. Both
checked=true
and
checked==true
don’t work.

//twotexts.js function strongsCheck(){ var KeyWord = "&keyword="; var chkStrongsBox = document.getElementsByClassName("checkbox-inline"); for(i=0;i<chkStrongsBox.length;i++){ if(chkStrongsBox[i].checked=true){ if(i<(chkStrongsBox.length-1)){ KeyWord += +chkStrongsBox[i].value+"|"; }else{ KeyWord += +chkStrongsBox[i].value; } } } alert(KeyWord); }

[code]


all Strongs’ in Common

<label class=“checkbox-inline” style=“width: 80px; height: 40px; display: block; font-weight: bold; color: red” title="
results:
(’ of their |fathers|,‘, ’ unto your |fathers|;’, ’ unto their |fathers|:‘, ’ of their |fathers|,’, ’ which their |fathers|‘, ’ themselves more than their |fathers|,’, ’ their |fathers|,‘, ’ therein, as their |fathers|’)

occuring 8 times as: 

('‘ab’, '‘ab’, '‘ab’, '‘ab’, '‘ab’, '‘ab’, '‘ab’, '‘ab’) - original language:(‘אָב’, ‘אָב’, ‘אָב’, ‘אָב’, ‘אָב’, ‘אָב’, ‘אָב’, ‘אָב’) - description: (‘a root’, ‘a root’, ‘a root’, ‘a root’, ‘a root’, ‘a root’, ‘a root’, ‘a root’)

related to:

()
also look at:
“>
[H1]

<label class=“checkbox-inline” style=“width: 80px; height: 40px; display: block; font-weight: bold; color: red” title=”
results:

occuring  times as: 
  • original language: - description:

    related to:
    ()
    also look at:
    “>
    [H4519]

    <label class=“checkbox-inline” style=“width: 80px; height: 40px; display: block; font-weight: bold; color: red” title=”
    results:

    occuring times as:

  • original language: - description:

    related to:
    ()
    also look at:
    “>
    [H27]

    <label class=“checkbox-inline” style=“width: 80px; height: 40px; display: block; font-weight: bold; color: blue” title=”
    results:
    (’ |after|‘, ’ ||‘, ’ |after|’, ’ ||’, ’ |after|‘, ’ |*|’)

    occuring 6 times as:
    ('‘achar’, '‘achar’, '‘achar’, '‘achar’, '‘achar’, '‘achar’) - original language:(‘אַחַר’, ‘אַחַר’, ‘אַחַר’, ‘אַחַר’, ‘אַחַר’, ‘אַחַר’) - description: (‘from אָחַר (H309)’, ‘from אָחַר (H309)’, ‘from אָחַר (H309)’, ‘from אָחַר (H309)’, ‘from אָחַר (H309)’, ‘from אָחַר (H309)’)

    related to:
    H309('achar)
    also look at:
    “>
    [H310]

    <label class=“checkbox-inline” style=“width: 80px; height: 40px; display: block; font-weight: bold; color: blue” title=”
    results:

    occuring times as:
    'achar - original language: - description:

    related to:
    ()
    also look at:
    ">

[/code]

The if statement is not testing if the checkbox is checked. Instead, it is testing if the equals assignment occurred successfully.

Move the assignment outside of the if statement and test for the checked property instead.

Or, make it a comparison instead and leave that comparison in the condition section of the if statement.

What do you mean by that? One thing I noticed as I posted is that the quotes and single apostrophes are conflicting.

Make them all double quotes for the sake of consistency. Sadly I cannot remain to help out further, but someone else should be able to assist you with your issue here.

when I alert this is what I get:
&keyword=NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN|NaN

What @Paul_Wilkins is telling you is that this code:

does not test to see if the box is checked as you probably intended. Instead, it assigns the box’s checked property a value of true and evaluates whether that assignment succeeded. If the assignment succeeds, the condition evaluates to true.

You are conflating the assignment operator (used to assign a value to a variable or property; a single =) with the equality operator (a double equals sign like ==) or the identity operator (sometimes called the strict equality operator; a triple equals like ===).

If you’re trying to test if the box is checked, you need to swap in one of these equality operators for your assignment operator.

1 Like

So what’s the best method of having a for loop with checkboxes? === ?

=== should work fine for the comparison you’re trying to do here. Actually, I might recommend this:

if(chkStrongsBox[i].checked){

Since if wants a true or false value and chkStrongsBox[i].checked will already evaluate to true if checked or false if not, no need to compare its value to anything.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.