ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

issue with jQuery IF-ELSE #280

Closed laburr27 closed 7 years ago

laburr27 commented 7 years ago

I tried:

var blueQ1=$('td[name="blueQ"]').attr("value");
//the value is "1"

var blueA1=$('button[name="bA1"]').attr("value"); 
//the value is "0"

$('button[name="bA1]"]').click(function(){
  if ("blueA1" == "blueQ1") {
    $("#blueFb").css("background-color", "green");
  } else {
    $("#blueFb").css("background-color", "red");
  }

I expected the background color to change to red.

The background color stayed the default (purple) instead.

My repo link is https://github.com/laburr27/trivial-pursuit

and my question is about lines 22 - 27 in script.js.

amaseda commented 7 years ago

@laburr27 You're comparing the strings "blueA1" and "blueQ1" -- not the variables -- inside of your if statement. Try removing the quotation marks.

laburr27 commented 7 years ago

Trying this using IDs instead of the NAME attribute. Got the first IF ELSE to work but now working on the second...

$("#bA1").click(function(){ // $("#blueFb").css("background-color", "green"); if ("blueA1" == "blueQ1") { $("#blueFb").css("background-color", "green"); } else { $("#blueFb").css("background-color", "red"); } });

$("#bA2").click(function(){ // $("#blueFb").css("background-color", "green"); if ("blueA2" == "blueQ1") { $("#blueFb").css("background-color", "green"); } else { $("#blueFb").css("background-color", "red"); } });

laburr27 commented 7 years ago

That did it!

So confusing when you need to use the quotes (") and when you don't...

Thanks!

amaseda commented 7 years ago

:+1:

Just know that if you surround it with quotes, you are not getting back the value stored in the variable. You are literally getting back the letters/words you have inside the quotes.