javascript - Using form input in jQuery function -


so trying allow user enter "access code" timestamp, , start countdown timer based on that. can set var manually , works, cannot form. missing?

html

<form>     <input type="text" name="access" onkeyup="formchanged()" onchange="formchanged()" />     <button type="submit" class="btn btn-default">submit</button> </form> 

jquery

$(function() {      function formchanged() {         var access = document.getelementsbyname("access")[0].value;     }      //var access = 1443564011;     var note = $('#note'),         // notice *1000 @ end - time must in milliseconds         ts = (new date(access * 1000)).gettime() + 1 * 24 * 60 * 60 * 1000;       $('#countdown').countdown({         timestamp: ts,         callback: function(days, hours, minutes, seconds) {              var message = "";              message += days + "<small class='white'>d</small>, " + access;             message += hours + "<small class='white'>h</small>, ";             message += minutes + "<small class='white'>m</small>, ";             message += seconds + "<small class='white'>s</small>";              note.html(message);         }     });  }); 

function formchanged() {     var access = document.getelementsbyname("access")[0].value; } 

you have problem of variable scope. when define variable access using var keyword inside function, accessible inside function. should move var statement out of function , should work.

var access = 0; function formchanged() {     access = parseint($("input[name=access]").val(), 10); } 

you add validation code make sure input number, nothing in current code handles case user doesn't input number.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -