Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Set variable onclick without separate script?

    Hi.

    Is there a way of assigning a value to a variable 'onclick' (of a button), but without having to use a separate script (<script> ... </script>) in the page?

    Also, can you use a variable to define the data path for, say, a flash object?
    Code:
     <object width="400" height="400" data=" <!-- here --> "></object>
    Thanks

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Answer to first question:
    Code:
    <button onclick="myvariable = 5;" />
    Answer to second question (if <object> is the first of its kind on the page):
    Code:
    var myvariable = "path/to/whatever";
    document.getElementsByTagName('object')[0].setAttribute("data", myvariable);

  • Users who have thanked devnull69 for this post:

    Coder34 (08-21-2012)

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you so much!

    I had failed to find the answer to either of my questions by searching the internet, and although the answer to the first one was probably staring me in the face the whole time, your help was much appreciated.

    With a bit of figuring I got the other to work as well - but only after I realised it had to go within a function, in a <script> tag (I am new to Javascript )

    Thanks again

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Nope, not true, at all. No need for <script> tag.

    Code:
    <html>
    <body>
    <object width="400" height="400" data=""></object>
    <input type="button" value="click to add data"
           onclick="var myvariable = 'path/to/whatever';
                   document.getElementsByTagName('object')[0].setAttribute('data', myvariable);"
    />
    </body>
    </html>
    Yes, you can put multiple statements and even multiple lines of JS code into an onclick (or onchange or or or) handler.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah yes, I think I see how it works now. For some reason I had in my head that you could only call functions or assign values to variables on a button click.

    Thanks

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Quote Originally Posted by Coder34 View Post
    Ah yes, I think I see how it works now. For some reason I had in my head that you could only call functions or assign values to variables on a button click.
    Ummm...yes. I think that's true. I mean you could just execute and expression to no purpose (e.g., onclick="3*4") but that's pretty pointless.

    So yes, my code assigns to one variable and invokes one function, so how is it different than what you thought you could do?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •