Can't use variables outside of function in javascript -
i started learn javascript , have prior experience in server side languages such php. issue i'm having cannot use variables defined outside of function inside function. have copy variable function in order code work. post example below.
var first = document.getelementbyid("first"); var second = document.getelementbyid("second"); function add () { alert(number(first.value) + number(second.value)); }
most problem script being executed when page still being loaded, , before "first" , "second" elements have been created.
accessing variables works fine. happen initialized "undefined" @ time created. moving variables inside function means aren't initialized until function called, after page has been loaded.
Comments
Post a Comment