Jump to content


[Javascript] Edit events


5 replies to this topic

#1 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 23 November 2007 - 21:44

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.

Posted Image
F O R T H E N S
Posted Image

#2 Felix Lockhart

    Visitor

  • Member
  • 22 posts

Posted 23 November 2007 - 22:24

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.


#3 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 23 November 2007 - 22:43

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.

Posted Image
F O R T H E N S
Posted Image

#4 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 23 November 2007 - 22:48

<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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#5 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 23 November 2007 - 23:09

best answer i heard so far :P
Posted Image
F O R T H E N S
Posted Image

#6 Felix Lockhart

    Visitor

  • Member
  • 22 posts

Posted 23 November 2007 - 23:12

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.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users