Forum Moderators: open

Message Too Old, No Replies

Prototype help

         

hOtTiGeR123

3:14 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Hi, I'm using prototype 1.6 as I'm trying to learn it. How would I achieve the following in prototype:

<a href="#" onmouseover="document.image.src='image2.gif';" onmouseout="document.image.src='image1.gif';">
<img src="image1.gif" name="image"/>
</a>

(basically a simple mouseover).

Thanks

astupidname

6:14 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



There are a few ways to go about it. Using my own img's and img paths, here is a demo of a couple ways using observe():

<!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></title>
<script type="text/javascript" src="prototype-1.6.0.3.js"></script>
<script type="text/javascript">
/*<![CDATA[*/ /*>fool my syntax hi-liter*/

function myRollover(event,path) {
var elem = event.element();
elem.src = path;
}

function chgImgBackTo_1m_gif(event) {
event.element().src = 'images/1m.gif'; //combine the two lines of code from myRollover
}

//initiate the elements when the window is fully loaded (same as doing window.onload = function () {//code};)
Event.observe(window, 'load', function () {
//does not use another function, the e receives the event automatically
$('image1').observe('mouseover', function (e) { e.element().src = 'images/2m.gif'; } );
//uses another function, passing arguments to it
$('image1').observe('mouseout', function (e) { myRollover(e,'images/1m.gif'); } );

//shorter, does not use another function. The 'this' object will be the event object.
$('image2').observe('mouseover', function () { this.src = 'images/2m.gif'; } );
//using callback function which requires no parameters other than event, which is automatically sent
$('image2').observe('mouseout', chgImgBackTo_1m_gif);
});

/*]]>*/
</script>
</head>
<body>
<p><a href="#"><img src="images/1m.gif" id="image1" alt="1m.gif" /></a></p>
<p><a href="#"><img src="images/1m.gif" id="image2" alt="1m.gif" /></a></p>
</body>
</html>

hOtTiGeR123

9:06 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Excellent many thanks sir! I assume I can use $$ for a class instead?

astupidname

5:02 am on Jan 15, 2009 (gmt 0)

10+ Year Member



You're welcome.

I assume I can use $$ for a class instead?

Yes, but you need to look into using .each() then, which I see you have picked up on in your other thread.