/    Sign up×
Community /Pin to ProfileBookmark

Rollover on a submit button

Hi All,

I am a javascript beginner and will really appreciate some help here. I have spent a considerable amount of time on a rollover function that never worked for me ?

All I am trying to do is have a rollover on a submit button on a html form. Ideally there should be an image on that button and when you rollover you see another image. However if this cannot be done it is ok to have the submit button as is and just see the 1 image when you rollover.

I have defined the images and the functions responsible for switching the images between in the head section of the html page. Here is the code:

<script language=javascript type=”text/javascript”>

if(document.images) {
img_off = new Image
img_on = new Image

img_off.src = “images/image1.gif”;
img_on.src = “images/image2.gif”;

}

function over(imageName)
{

if (document.images)
{
document[imageName].src = eval(imageName+”_on.src”);
}

}

function out(imageName)
{
if(document.images)
{
document[imageName].src = eval(imageName+”_off.src”);
}

}

</script>

I Then call my functions within the submit cell in the form as follows:

<td><input type=submit onmouseover=”over(‘image’)” onmouseout=”out(‘image’)” <img src=”../images/image2.gif” value=”continue” ></td>

This function call does not make sense to me, I took it from a javascript text book, I know there is something missing but I don’t know, I am also unsure if I am putting this code in the right place.

Thank you ?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMay 26.2005 — &lt;input type=submit onmouseover="over('image')" onmouseout="out('image')"[color=red]&gt;[/color]
Copy linkTweet thisAlerts:
@UltimaterMay 26.2005 — I'd personally re-write the whole thing to:
<i>
</i>&lt;td&gt;&lt;input type=submit onmouseover="over('image')" onmouseout="out('image')"&gt; &lt;img src="../images/image2.gif" value="continue" id="img_1"&gt;&lt;/td&gt;


<i>
</i>&lt;script language=javascript type="text/javascript"&gt;
if(!document.getElementById){if(document.all)document.getElementById=function(){if(typeof document.all[arguments[0]]!="undefined")return document.all[arguments[0]];else return null}else if(document.layers)document.getElementById=function(){if(typeof document[arguments[0]]!="undefined")return document[arguments[0]];else return null}}

img_off = new Image();
img_on = new Image();
img_off.src = "images/image1.gif";
img_on.src = "images/image2.gif";

function over(imageName){
document.getElementById("img_1").src = window[imageName+"_on"].src
}

function out(imageName){
document.getElementById("img_1").src = window[imageName+"_on"].src
}

&lt;/script&gt;
Copy linkTweet thisAlerts:
@memeauthorMay 26.2005 — thanks for that but it doesn't work, here is what happen when i use your code:

- the submit button becomes "submit query"!

- nothing happens when I hover over it

- I get error on the page in my browser (where it usually says done at the bottom left hand corner)

- the error says line 31, char 3, Error: document[.....] is null or not an object

- line 31 is the following line out of the code I sent earlier I sent earlier:

function out(imageName)

I am sorry this must be one of the easiest things in javascripting, but I hope you appreciate it is not easy to me as I am just learning javascript.

thanks ?
Copy linkTweet thisAlerts:
@memeauthorMay 26.2005 — Now, I tried this code from Ultimater, I see my second image showing after the "submit query" button (I have no idea where this button came from)

and nothing happens when I hover over the button.

Strange enough the error now points to the form field just before the submit button.

Help me please




I'd personally re-write the whole thing to:
<i>
</i>&lt;td&gt;&lt;input type=submit onmouseover="over('image')" onmouseout="out('image')"&gt; &lt;img src="../images/image2.gif" value="continue" id="img_1"&gt;&lt;/td&gt;


<i>
</i>&lt;script language=javascript type="text/javascript"&gt;
if(!document.getElementById){if(document.all)document.getElementById=function(){if(typeof document.all[arguments[0]]!="undefined")return document.all[arguments[0]];else return null}else if(document.layers)document.getElementById=function(){if(typeof document[arguments[0]]!="undefined")return document[arguments[0]];else return null}}

img_off = new Image();
img_on = new Image();
img_off.src = "images/image1.gif";
img_on.src = "images/image2.gif";

function over(imageName){
document.getElementById("img_1").src = window[imageName+"_on"].src
}

function out(imageName){
document.getElementById("img_1").src = window[imageName+"_on"].src
}

&lt;/script&gt;
[/QUOTE]
Copy linkTweet thisAlerts:
@UltimaterMay 26.2005 — Give this a shot:
&lt;td&gt;&lt;img onmouseover="over('img')" onmouseout="out('img')" src="../images/image2.gif" id="img_1"&gt;&lt;/td&gt;
Copy linkTweet thisAlerts:
@memeauthorMay 26.2005 — Tried, now that "submit query" button has disappeard and I got the image displayed. However nothing happens when I hover over the image. I am now unable to submit my form (i.e. go to the next page).

I think this last code removes the functionality of the submit button. I need the submit functionality because I have to submit the form contents to the next page (I have some php code on the next page that requires the data posted from a form). Or does that make no difference and all it matters is (method = "POST") within the form tag?


cheers
Copy linkTweet thisAlerts:
@UltimaterMay 26.2005 — Forget everything that I have said up until now can try replacing:
<i>
</i>if(document.images) {
img_off = new Image
img_on = new Image

img_off.src = "images/image1.gif";
img_on.src = "images/image2.gif";
}

with:
<i>
</i>if(document.images) {
[color=royalblue]image[/color]_off = new Image
[color=royalblue]image[/color]_on = new Image

[color=royalblue]image[/color]_off.src = "images/image1.gif";
[color=royalblue]image[/color]_on.src = "images/image2.gif";
}
Copy linkTweet thisAlerts:
@memeauthorMay 27.2005 — Hi again,

this still does not work.

I now found the following code which calls the submit method of the form. I used it and I am able to display the first image instead of the submit Button, however, nothing happens whan I hover over the button. Furthermore, I loose all my form validation functionality when I use this code. Any idea why?

here is the code:

[code=php]<td><A HREF=" " onMouseover="over('image')" onMouseout="out('image')"><img src="../images/image1.gif" alt="" name="image1" height="24" width="52" border="0" onclick="document.FORMNAME.submit();"></A></td>[/code]

Many thanks
×

Success!

Help @meme spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...