Link to home
Start Free TrialLog in
Avatar of Andrew99
Andrew99

asked on

Submit rollover button that works in MOZ and IE

HI, I have been having this big problem.
I made some rollovers in CSS and they don't work in MOzillia, so I tried javascript rollovers, the problem is that the javascript rollovers is when I the form is submitted I have it validate the input box fields but the javascript code for the form validatation does not execute when I use javascript rollovers.

Is there a simple way I can have a rollover that acts like a submit button that will work in both Mozillia and IE?
Avatar of J-Mik
J-Mik

Andrew99,

Just create an a normal IMG rollover and add something along these lines: onClick="if (MyValidationFunction) submit()";
Avatar of Andrew99

ASKER

What do you mean by normal img rollover?
can you give me a example of a simple image rollover?
Will something like this work?  I created a very simple form, with an input of type image.  When you click on this image, the form submits to the same page.  You can tell the form submitted because you'll see "hiddenInput=peek-a-boo" in the URL/query string.  As for the roll over effect, you can achieve it with a bit of CSS.  The only javascript you need is to make up for IE's poor implementation of the CSS, so that you can have your mouseover/mouseout effect.  Obviously the styling is very simple, but you can change it however you need.  Hope that helps.

<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<style type="text/css">
#theImg {
  border: 0;
  height: 50px;
  width: 50px;
}

#theImg:hover, #theImg.myHover {
  border: 1px solid blue;
  padding: 2px;
  background-color: #bdb;
}
</style>
<script type="text/javascript">
window.onload = init;

function init()
{
  var obj = document.getElementById("theImg");
  obj.onmouseover = function() {this.className = "myHover";};
  obj.onmouseout = function() {this.className = "";};
}
</script>
</head>

<body>
<form id="formToSubmit" name="formToSubmit" method="get">
  <input type="hidden" name="hiddenInput" value="peek-a-boo" />
  <input type="image" src="smiley.jpg" id="theImg" />
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of J-Mik
J-Mik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I need the submit to submit as post and not like a Get which shows up the in query string.
Change the <form> tag's method to "post".  The <input type="image" ...> tag will submit the form the same way that <input type="submit" ...> would - you still have to define a method and an action for the form itself.  I used the "get" method to illustrate that it does in fact submit, but there's no reason why you can't change it to "post".  Hope that helps.
Dakyd your code is good excpet how do i implement the rollover?
What exactly do you want to happen when you mouse over the image?  I assumed (maybe incorrectly) that you just wanted to style the image, rather than change the image.  If that's the case, then all you have to do is use my last post and add your styling to the CSS block for #theImg:hover and #theImg.myHover.

However, if you want the picture to change, then you essentially use J-Mik's code.  To change the image when you mouseover it, you want something like the following.  Take out the hidden input if you don't need it, and you'll notice that I changed the method to "post" this time.  I also styled the image to be 100px by 100px, but that's also not necessary, you can take it out or adjust the dimensions however you want.  Either way, hope that helps.

<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<style type="text/css">
#theImg {
  height: 100px;
  width: 100px;
}
</style>
<script type="text/javascript">
window.onload = init;

img1 = new Image();
img2 = new Image();
img1.src = "sad.jpg";     // PUT YOUR ONMOUSEOVER IMAGE SRC HERE
img2.src = "smiley.jpg"; // PUT THE ORIGINAL IMAGE SRC HERE

function init()
{
  var obj = document.getElementById("theImg");
  obj.onmouseover = function() {this.src = img1.src;};
  obj.onmouseout = function() {this.src = img2.src;};
}
</script>
</head>

<body>
<form id="formToSubmit" name="formToSubmit" method="post">
  <input type="hidden" name="hiddenInput" value="peek-a-boo" />
  <input type="image" src="smiley.jpg" id="theImg" />
</form>
</body>
</html>
J-Mik you gave me exactily what I was looking for something nice and simple!

And it worked.  I just put the onMouseOver="document.getElementById('MyImg').src = ImgOn.src;" onMouseOut="document.getElementById('MyImg').src = ImgOff.src;"

in the input box using the javascript code and everything fit together so perfect!

Thanks for your help.
Glad I could help!