javascript - External file java script does not function -
my code not function in external file <script src="action_input.js"></script>
.
put code in <body></body>
or <head></head>
, not function.
my code functions in <body><script> code js</script></body>
, not so.
code is:
// identify form elements: var search_code = document.getelementbyid('search_code'); var insert_code = document.getelementbyid('insert_code'); var result = document.getelementbyid('result'); var button = document.getelementbyid('button'); var audio = new audio('sound.wav'); // respond button click button.onclick = function validate() { // show verification result: if(search_code.value == insert_code.value) { result.textcontent = 'cod gasit'; result.classname = "ok"; audio.play(); //http://soundbible.com/tags-winning.html } else { result.textcontent = 'codul nu este corect'; result.classname = "not-ok"; } // clear input when wrong: if (search_code.value !== insert_code.value) { insert_code.value = ''; } return false; }; //sterge textul cand se da click pe input function clearfield(input) { input.value = ""; };
my input is:
<form> <input type="text" name="search_code" onfocus="clearfield(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value=""/><br/> <input type="" name="insert_code" onfocus="clearfield(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="scaneaza codul" autocomplete="off" value=""/><br/><br/> <input type="submit" id="button" name="button" value="verifica cod" /> </form>
use
e.preventdefault();
prevent default action ofsubmit button
or usetype='button'
there no element having id result
try this:
var search_code = document.getelementbyid('search_code'); var insert_code = document.getelementbyid('insert_code'); var result = document.getelementbyid('result'); var button = document.getelementbyid('button'); var audio = new audio('sound.wav'); // respond button click button.onclick = function validate(e) { e.preventdefault(); // show verification result: if (search_code.value == insert_code.value) { result.textcontent = 'cod gasit'; result.classname = "ok"; audio.play(); //http://soundbible.com/tags-winning.html } else { result.textcontent = 'codul nu este corect'; result.classname = "not-ok"; } // clear input when wrong: if (search_code.value !== insert_code.value) { insert_code.value = ''; } return false; }; //sterge textul cand se da click pe input function clearfield(input) { input.value = ""; };
<form> <input type="text" name="search_code" onfocus="clearfield(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value="" /> <br/> <input type="" name="insert_code" onfocus="clearfield(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="scaneaza codul" autocomplete="off" value="" /> <br/> <br/> <div id="result"></div> <input type="submit" id="button" name="button" value="verifica cod" /> </form>
Comments
Post a Comment