1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can I assign a variable a value onclick?

Discussion in 'JavaScript' started by jjfletch, Jul 30, 2008.

  1. #1
    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" />
    SEMrush
    Possible?
     
    jjfletch, Jul 30, 2008 IP
    SEMrush
  2. VishalVasani

    VishalVasani Peon

    Messages:
    560
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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):
     
    VishalVasani, Jul 30, 2008 IP