Is it possible to assign a value to a variable onclick? I know that I can assign a value to an input field, but I wanted something where I could do this: var mysample = ''; <input type="button" id="sample0" onclick="mysample is assigned the value of 0" /> Possible?
Hello Yes it possible check below code : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <script> var mysample = ''; function setval(varval) { mysample= varval; alert(mysample); } </script> <body> <input type="button" id="sample0" onclick="javascript:setval(0)" /> <marquee id="Marquee1">Hello</marquee> </body> </html> Code (markup):
Yep, I'd go about it within the onclick attribute, unless you intend to use it multiple times, then place it into a function and call the function w/ onclick onclick="this.value='my sample is assigned a value of '+x.toString();" Code (markup):