Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-20-2012, 12:09 PM   PM User | #1
Coder34
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Coder34 is an unknown quantity at this point
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
Coder34 is offline   Reply With Quote
Old 08-20-2012, 12:26 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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);
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
Coder34 (08-21-2012)
Old 08-21-2012, 09:03 PM   PM User | #3
Coder34
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Coder34 is an unknown quantity at this point
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
Coder34 is offline   Reply With Quote
Old 08-21-2012, 10:12 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 08-24-2012, 07:11 PM   PM User | #5
Coder34
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Coder34 is an unknown quantity at this point
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
Coder34 is offline   Reply With Quote
Old 08-24-2012, 10:12 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
flash, object, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:33 AM.


Advertisement
Log in to turn off these ads.