javascript - Why does this code need a second if statement? -


i'm trying understand why 1 version of code works , 1 not. hope i've included enough information. i'm taking online tutorial no longer supported.

in tutorial, instructions write code this:

$(document).ready(function(){     $("form").submit(function() {     var input = $("#command_line").val();     console.log(input)     console.log("submit")      if (input === "help") {         $("#message_help").clone().insertbefore("placeholder").fadein(1000);     } });  $("#command_line").val("");  }); 

there no error messages in console, expected happen in browser doesn't execute.

in working code, there if statement above original:

$(document).ready(function(){     $("form").submit(function() {     var input = $("#command_line").val();     console.log(input);       if (input.indexof("help") > -1) {         if (input == "help") {             $("#message_help").clone().insertbefore("#placeholder").fadein(1000);         }     }        });  $("#command_line").val("");  }); 

why need second if statement using indexof on string 'help'? looked indexof, , understand does, have thought both same thing?

it's typo in first example. @ line in second example:

$("#message_help").clone().insertbefore("#placeholder").fadein(1000); 

you have insertbefore("#placeholder"), looks element id of "placeholder" , inserts clone before it.

in first example you've skipped #, it's looking tag of "placeholder" (eg: <placeholder>) - doesn't exist. hence element cloned, inserted after nothing.

  • $('div') return <div>s on page.
  • $('.pie') return elements on page class of "pie"
  • $('#pie') return element on page id of "pie".

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -