user1832254
user1832254

Reputation: 21

Checking checkboxes are checked in an IF statement

I am currently having trouble making my IF statement work I want a user who selects shield "ely" and either selects textbox 3 or 4 would then automatically tick textbox 2.

This is my JavaScript code:

if(showShield.value == ("ely") && document.getElementById("cb3").checked = true; || document.getElementById("cb4").checked = true;)
    {
    document.getElementById("cb2").checked=true;
        }

This is probably wrong since it does not work so how would I be able to check is a user selects textbox 3 or 4.

Here is my HTML code:

<html>
<head>
<script type="text/javascript" src="myJavascript.js"></script>
</head>
<body>

<select id="likeShield" onchange="showTicks()">
<option value="select1">Select</option>
<option value="yesShield">Yes</option>
<option value="noShield">No</option>
</select>

<select id="showShield" onchange="showTicks()">
<option value="select1">Select</option>
<option value="arc">Arcane</option>
<option value="ely">Elysian</option>
<option value="spec">Spectral</option>
<option value="anylist">Choose any</option>
</select>

<table border = "1">
<tr>
   <th> tickbox </th>
   <th> shield parts </th>
   <th> description </th>
   <th> cost </th>
</tr>
<tr>
<td><input type="checkbox" id="cb1"></td>
   <td> arc sigil </td>
   <td> Large magic part </td>
   <td> 5m </td>
</tr>
<tr>
<td><input type="checkbox" id="cb2"></td>
   <td> arc shield </td>
   <td> A extremely powerful magic shield </td>
   <td> 60m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb3"></td>
   <td> arc special item </td>
   <td> special element </td>
   <td> 10m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb4"></td>
   <td> elysian sigil  </td>
   <td> A sigil found by dragons </td>
   <td> 50m </td>
</tr>   
<tr>
   <td><input type="checkbox" id="cb5"></td>
   <td> elysian shield </td>
   <td> A extremely powerful ranging shield </td>
   <td> 40m </td>
</tr>   
<tr>
   <td><input type="checkbox" id="cb6"></td>
   <td> elysian special item </td>
   <td> A special attack attached to shield </td>
   <td> 25m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb7"></td>
   <td> spectral sigil  </td>
   <td> easily obtainable from goblins </td>
   <td> 4m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb8"></td>
   <td> spectral shield </td>
   <td> Impressive stats </td>
   <td> 15m </td>
</tr>
<tr>
   <td><input type="checkbox" id="cb9"></td>
   <td> spectral special item </td>
   <td> Does double damage </td>
   <td> 30m </td>
</tr>
</table>
</body>
</html> 

Upvotes: 2

Views: 31543

Answers (7)

Ahmer
Ahmer

Reputation: 94

javascript checkbox if else condition

$checkbox = document.getElementById("checkBoxId").checked;

if($checkbox == true) {
    //check true
} else {
    //check false
}

jQuery checkbox if else condition

$checkbox = $("#checkBoxid").is(":checked");

if($checkbox == true) {
     //check true
} else {
     //check false
}

Upvotes: 0

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46738

if(showShield.value == "ely" && (document.getElementById("cb3").checked || document.getElementById("cb4").checked))//comparison
{
    document.getElementById("cb2").checked=true; //asignment
}
  1. Operator Precedence: && is done before || so you need brackets
  2. ==, not = for comparison (Preferably === which will not automatically convert types and do a strict comparison)
  3. ; is a statement terminator and shouldn't be used in the middle of one.

Upvotes: 3

kannanrbk
kannanrbk

Reputation: 7134

if(showShield.value == ("ely") && document.getElementById("cb3").checked = true; || document.getElementById("cb4").checked = true;)

showShield.value == ("ely") && document.getElementById("cb3").checked = true; js checked the condition as the showShield.value must be ely and cb3 is checked due to operator precedence then it checked the 'OR' statement cb4 is checked .

We can solve this by .

    if(showShield.value == ("ely") && (document.getElementById("cb3").checked = true; || document.getElementById("cb4").checked = true))

Upvotes: 0

username tbd
username tbd

Reputation: 9382

There may be quite a bit wrong here (as you didn't provide too much information here), but at the very least, instead of

document.getElementById("checkBox3").checked = true;

You should have

document.getElementById("checkBox3").checked == true

The same goes for "checkBox4".

Here's a quick explanation why:

1) When you use one equal sign, you are setting a value. So instead of testing if checkBox3 is checked, you're essentially telling checkBox3 to become checked. Using two equal signs is necessary for if statements, since it makes the statement a question instead of a command.

2) Semicolons are only necessary at the end of a statement. Since the "if" line is one whole statement, you don't want semicolons inside of it.

Hope that makes sense!

Upvotes: 1

Vyacheslav Voronchuk
Vyacheslav Voronchuk

Reputation: 2463

if(showShield.value === "ely" && (document.getElementById("cb3").checked == true || document.getElementById("cb4").checked == true))

No need to use semicolon and use == instead of =. Also brackets are needed for condition.

Upvotes: 1

Anton Egorov
Anton Egorov

Reputation: 1372

You would probably want:

if(ely.value == ("ely") && document.getElementById("checkBox3").checked == true || ely.value == ("ely") && document.getElementById("checkBox4").checked == true)
{
document.getElementById("checkBox2").checked=true;
    }

or

if(ely.value == ("ely") && (document.getElementById("checkBox3").checked == true || document.getElementById("checkBox4").checked == true))
{
document.getElementById("checkBox2").checked=true;
    }

It's because logical operators have their order of comparison, just like arithmetical multiplication has its priority over addition. Well, I'm new to JS, but I believe it's how it works everywhere. Moreover you have to use double equality sign to compare, because single is used for assigning the value.

Upvotes: 0

IgorCh
IgorCh

Reputation: 2661

Try this

if(ely.value == "ely" && (document.getElementById("checkBox3").checked ||
  document.getElementById("checkBox4").checked))
{
    document.getElementById("checkBox2").checked=true;
}

Upvotes: 0

Related Questions