javascript - Button not calling a function -
i creating simple array counter , sorter school , have encountered problem count button calling count() function. here's code.
<!doctype html> <html> <head> <script language=javascript> var products = ["printer","tablet","router","keyboard","headset"]; function sort() { products.tostring() document.getelementbyid("result").innerhtml = "unsorted array = " + products; products.sort(); products.tostring(); document.getelementbyid("result2").innerhtml = "sorted array = " + products; } function count() { document.getelementbyid("count").innerhtml = "the amount of items in array is: " + fruit.length; } </script> <title>task 2</title> </head> <body> <input type="button" name="butt1" value="sort!" onclick="sort()"/> <input type="button" name="butt2" value="count!" onclick="count()"/> <p id="result"></p> <p id="result2"></p> <p id="count"></p> </body> </html>
that because fruit
not visible count function
i guess want use products
here
function count() { document.getelementbyid("count").innerhtml = "the amount of items in array is: " + products.length; }
or if want use fruit
, need declare , initialize globally
Comments
Post a Comment