Home Ask Login Register

Developers Planet

Your answer is one click away!

Sterica St February 2016

External file java script does not function

My code does not function in external file <script src="action_input.js"></script>.
I put this code in <body></body> or <head></head> and it does not function.

My code functions in <body><script> code js</script></body>, but not like so.
My 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 to 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" />        

Answers


Rayon Dabre February 2016

Use e.preventDefault(); to prevent default action of submit button or use type='button'

There is 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 to 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>

Post Status

Asked in February 2016
Viewed 2,380 times
Voted 6
Answered 1 times

Search




Leave an answer


Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved

Search