←  Code Block

Fallout Studios Forums

»

[Javascript] Edit events

Slightly Wonky Robob's Photo Slightly Wonky Robob 23 Nov 2007

So yeah, im starting to learn javascript... i've tried in the past but i've always found its too different to langauges i'm used to...

Anyway long story short, I'm playing with a few simple functions and i'm trying to make something that the will change the "onmouseover" and "onmouseout" events of (in this case) an image. Now primarily i presumed it would be the same as changing the source of an image, e.g.
img.src="image.jpg";

However it didn't seem to work.. and after a little investigation on the interweb, it appears the format for changing events of an object is slightly different... from what i gather you have to use 'attachevent'... now i've tried this, but i can't seem to get it to work, so help on this would be apreciated...

Now this brings me nicely on to my second point. Is there a good javascript resource out there, listing all the functions that can be used.. with examples, etc. Something like the one for php; http://php.net/

Thanks in advance :P
Edited by Bob, 23 November 2007 - 21:46.
Quote

Felix Lockhart's Photo Felix Lockhart 23 Nov 2007

Unfortunately, there isn't an official JS reference like the one for PHP, but they are out there. I usually use this one: http://www.w3schools.../js/default.asp

EDIT: I just re-read your post, and I honestly don't know how to do what it sounds like you're trying to do. Basically, you're trying to dynamically change the event properties of an object? I'll ask my JS professor, he'll probably know. Off the top of my head, I'd probably use something like OuterHTML to rewrite the tags. Is there a specific HTML element you're trying to do this to?
Edited by Felix Lockhart, 23 November 2007 - 22:31.
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 23 Nov 2007

i've tried that site before... not very good for looking up specific functions :cool:

and yeah.. i'm not sure if i made myself entirely clear with exactly what i want to do... here is my explanation to kassad... although it probably just confuses things more :P

Quote

[22:02:42] Bob: <img src="image.jpg" onmouseover="this.src='image_hvr.jpg';" onmouseout="this.src='imager.jpg';">

i then want a third function, for example, to change the onmouseover bit to this.src='image_hvr2.jpg';


kassad has given me a few few alternatives that i had already considered myself...

such as 2 seperate images with the different onmouse events, and simply hide/make visible and secondly, create an if statement in the original onmous event, if a variable is 1 use such and such, if not use something else

But i was hoping that mt original idea was possible... i don't see why it should be any different than changing any other property.. but it obviously is :)
Edited by Bob, 23 November 2007 - 22:46.
Quote

CodeCat's Photo CodeCat 23 Nov 2007

<script type=&#34;text/javascript&#34;>

var imageToLoad = &#34;image_hvr.jpg&#34;;

</script>

<img src=&#34;image.jpg&#34; onmouseover=&#34;this.src = imageToLoad;&#34; onmouseout=&#34;this.src=&#39;imager.jpg&#39;;&#34; />


Then all you do is set imageToLoad to another value whenever it needs changing.
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 23 Nov 2007

best answer i heard so far :P
Quote

Felix Lockhart's Photo Felix Lockhart 23 Nov 2007

View PostCodeCat, on 23 Nov 2007, 17:48, said:

<script type=&#34;text/javascript&#34;>

var imageToLoad = &#34;image_hvr.jpg&#34;;

</script>

<img src=&#34;image.jpg&#34; onmouseover=&#34;this.src = imageToLoad;&#34; onmouseout=&#34;this.src=&#39;imager.jpg&#39;;&#34; />


Then all you do is set imageToLoad to another value whenever it needs changing.


Ahh, I get it now. I second CC's suggestion, it'd be far easier than trying to actually change the attribute.
Quote