javascript - Using an array inside of a function to store names -
javascript - Using an array inside of a function to store names -
i trying practice javascript , read little challenge on website making little scheme users can follow , unfollow 1 another. still new javascript , programming in general please forgive ignorance. below came with. trying create off top of head because might best way larn language imo.
basically in little function, created empty array set in function. set function onclick handler in tag. way store names or things in general? on right track little task?
<!doctype html> <html> <head> </head> <body> <input id="searchbox" value = "names"> <button type="radio" onclick="listofpeople();"> <script type="text/javascript"> "use strict"; function listofpeople (){ var storedpeople = []; listofpeople(); }; </script> </body> </html>
since you've declared storedpeople array within listofpeople function, limit "scope" can add together or remove items listofpeople function.
something not work setup currently, because storedpeople array is in "closure" because declared within function.
<script type="text/javascript"> function listofpeople (){ var storedpeople = []; listofpeople(); }; alert(storedpeople[0]); </script>
this should show how declare array outside of function has "wider scope", meaning can accessed more within of listofpeople function.
<!doctype html> <html> <body> <input id="searchbox" value = "names"> <button type="radio" onclick="listofpeople();"> <script type="text/javascript"> // declare storedpeople array outside of function var storedpeople = []; // modify array in function function listofpeople (){ storedpeople.push(document.getelementbyid("searchbox").value); }; </script> </body> </html>
javascript html
Comments
Post a Comment