Javascript examples for jQuery:Mouse Event
Set the global variable in click event
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://code.jquery.com/jquery-compat-git.js"></script> <script type="text/javascript"> $(window).on('load', function() { $(document).ready(function() { var x = ""; $("#my").click(function() { x = "test"; console.log(x); });/*ww w.j av a2 s. c om*/ console.log(x); }); }); </script> </head> <body> <button id="my">hihi</button> </body> </html>