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" />

    Possible?
     
    jjfletch, Jul 30, 2008 IP
  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
  3. scu8a

    scu8a Greenhorn

    Messages:
    81
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    15
    #3
    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):
     
    Last edited: Dec 18, 2019
    scu8a, Dec 18, 2019 IP