jquery - live form results using javascript -


i trying provide message below fields of form. message depend on entered in both fields.

how go making calculates in real time using both fields , passing through calculation?

here fiddle http://jsfiddle.net/6qseh/

i using document values

var input1 = document.getelementbyid("input-mini"); var input2 = document.getelementbyid("input-mini2"); 

and @ end run function

yearcalculator(); 

there many missing pieces in code.

firstly have written code entirely using javascript , trying use jquery syntax. how expect work.

jquery set html     ---   msg.html(value); javascriptto set html  ---   msg.html = value; 

second when checking not number

it supposed like

val1 === nan // not string never work nan never equal nan

use isnan() method instead

third

<div class="message"></div> 

supposed be

<div id="message"></div> 

next need assign events input. otherwise work when page loads first time..

input1.addeventlistener('change', yearcalculator); input2.addeventlistener('change', yearcalculator); 

otherwise work first time script loads.

cleaned code

var input1 = document.getelementbyid("input-mini"); var input2 = document.getelementbyid("input-mini2"); var msg = document.getelementbyid('message');  input1.addeventlistener('change', yearcalculator); input2.addeventlistener('change', yearcalculator);  function yearcalculator() {    var yearone = input1.value;     var yeartwo = input2.value;     val1 = parseint(yearone);     val2 = parseint(yeartwo);     if (isnan(val1) || isnan(val2)) {         msg.innerhtml = "please enter valid year !!";         return;     }     var value1 = yearone - yeartwo + 18;      if (yearone == yeartwo) {         msg.innerhtml = "both years same";     }      if (yearone < yeartwo) {         if (yeartwo < value1) {             msg.innerhtml = "this result";         } else if (yeartwo > value1) {             msg.innerhtml = "this bad result";         } else {             msg.innerhtml  = "this neither or bad";         }     }     else {       msg.innerhtml ="year 1 greater year 2";       } }; yearcalculator(); 

check fiddle


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -