Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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?
0
Andrew99
Asked:
Andrew99
  • 5
  • 3
  • 3
1 Solution
 
J-MikCommented:
Andrew99,

Just create an a normal IMG rollover and add something along these lines: onClick="if (MyValidationFunction) submit()";
0
 
Andrew99Author Commented:
What do you mean by normal img rollover?
0
 
Andrew99Author Commented:
can you give me a example of a simple image rollover?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dakydCommented:
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>
0
 
J-MikCommented:
Andrew99,

Here's a sample of a simple image rollover:

<SCRIPT TYPE="text/javascript">
<!--

  var ImgOff = new Image();
  var ImgOn = new Image();
 
  ImgOff.src = "SomeOffImage.jpg";
  ImgOn.src = "SomeOnImage.jpg";

//-->
<SCRIPT>

<IMG ID="MyImg" SRC="SomeOffImage.jpg" onMouseOver="document.getElementById('MyImg').src = ImgOn.src;" onMouseOut="document.getElementById('MyImg').src = ImgOff.src;">


...And then of course, you can add the onClick code as well. Let me know if you have any problems with this.
0
 
Andrew99Author Commented:
I need the submit to submit as post and not like a Get which shows up the in query string.
0
 
dakydCommented:
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.
0
 
Andrew99Author Commented:
Dakyd your code is good excpet how do i implement the rollover?
0
 
dakydCommented:
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>
0
 
Andrew99Author Commented:
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.
0
 
J-MikCommented:
Glad I could help!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now