jeudi 3 août 2017

why is jquery and if statements not working together

My code is supposed to hide several divs on load but for some reason when I type in the code that is supposed to bring up a div nothing happens. Please tell me why does the jquery in my code not work with my if-statements.

here is the javascript/jquery

var code = $("#code");

function onstart(){
  $("#superfoods").hide();
  $("#fakemedia").hide();
  $("#dropbear").hide();
}

function checkcode(){
  if (code == "superfoods")
  { 
    $("#superfoods").show();
  }
  else if (code == "fakemedia")
  {
    $("#fakemedia").show();
  }
  else if (code == "dropbear")
  {
    $("#dropbear").show();
  }
  else {
    document.alert( code + "is not a valid imput.")
  }
}

and here is the HTML (just in case)

<body onload="onstart()">
    <input id="code"><button onclick="checkcode()">search for level</button>
    <div id="superfoods">
        <a href="superfoods.html"><img src="super-food-400x400.jpg"></a>
    </div>
    <div id="fakemedia">
        <a href="fakemedia.html"><img src="fakemedia.png"></a>
    </div>
    <div id="dropbear">
        <a href="dropbear.html"><img src="drop-bearfgh.jpg"></a>
    </div>
</body>

Aucun commentaire:

Enregistrer un commentaire