views:

541

answers:

3

Solved! Thanks guys!

Okay, in a form I have a button 170x60 with a custom image for the non-pressed and pressed states. What I can't seem to do is get There to be text in the button. I really want to avoid having to edit the image and write text to it directly. Right now I'm using

<input type="image" src="img/button_normal.png" width="175" height="60" onmousedown="this.src='img/button_pressed2.png'" onmouseup="this.src='img/button_normal.png'">

I figured that it is impossible to make this work with text on top of it (as far as I have tried)

So then I tried using <button> but I couldn't get it to work either.

So is there a solution to my problem?

edit: I should have mentioned that I have been able to put text over the image above, but where the text is, the button is unclickable, which doesn't work.

+1  A: 

So why not just edit the background image on a regular button?

<script type="text/javascript" src="picker/js/jquery.js"></script>
<!-- some time later -->
<input type="button" src="img/button_normal.png" 
width="175" height="60" value="Some Text"
onmousedown="$(this).css({'background-image': 'img/button_pressed2.png'});" 
onmouseup="$(this).css({'background-image': 'img/button_normal.png'});">

That should give you the ability to put an image into a button with some text overlaid.

Incidentally, you COULD do the same thing in pure JS, but the $ operator alone will save you a ton of time on any half-way serious project.

jQuery, it's what's for breakfast.

Inaimathi
<facepalm /> Forgot to mention, set the value property of the input to whatever text you want to show. Fixed above.
Inaimathi
Doing that just made a normal button with the value text :/
Scotta
A: 

Why not just use css and a "submit" type?

<input type="submit" class="myButton" value="Label goes here" />

Then in your CSS:

input.myButton{
     background-image: url('img/button_normal.png');
     border-style:none;
}
input.myButton:hover{
    background-image: url('img/button_pressed2.png');
}
input.myButton:active{
    background-image: url('img/button_normal.png');
}

This will work just as well with "button" type, and has an advantage over Javascript versions because Javascript doesn't have to be enabled.

To make this work properly in IE, you can use this script to make IE behave itself :)

Brendan Long
That doesn't display the image at all. Just a normal looking button with my value inside of it.
Scotta
What browser are you using? I tested it in Firefox and Epiphany (webkit-based). I don't have IE :-\
Brendan Long
Here's an example: http://supremerule.net/testing/button-test.html
Brendan Long
Not crossbrowser compatible. IE lte 7 doesn't support those pseudoselectors.
BalusC
I updated it with a link to a script that makes IE work correctly.
Brendan Long
A: 

The <input type="image"> is meant to have sort of an image map as button. When submitted, it sends a name.x and name.y parameter to the server side which denotes the exact location where the client has pressed on the image map.

You obviously don't want to have that. You just want to have a button with a background image. For that you normally use the CSS background-image property for (or, more conveniently, the background property).

Basic example:

.mybutton {
    background-image: url('normal.png');
    width: 175px;
    height: 60px;
}
.mybutton.pressed {
    background-image: url('pressed.png');
}

with the normal button as follows:

<input 
    type="submit"
    class="mybutton" 
    value=""
    onmousedown="this.className+=' pressed'"
    onmouseup="this.className=this.className.replace(' pressed', '')"
>

Edit: for downvoters or JS/jQuery nitpickers: IE lte 7 does not support the :hover nor :active pseudoselectors on other elements than the a element. The above solution is crossbrowser compatible.

BalusC
The javascript in this is superfluous. If you just make the second CSS class .mybutton:active, it has the same effect but without requiring Javascript.
Brendan Long
Why are you downvoting for a crossbrowser compatible solution? IE lte 7 doesn't support those pseudoselectors.
BalusC
Oh, good then. It worked, Thanks!
Scotta
Hm I forgot about that. There are better solutions than using Javascript like this though: http://www.xs4all.nl/~peterned/csshover.html
Brendan Long
Brendan, that's **also** a Javascript solution. It is only a bit more advanced so that it get applied on all elements automagically.
BalusC
Look, you can even see the source here: http://www.xs4all.nl/~peterned/htc/csshover2.htc
BalusC
I don't think IE likes having a background image for buttons. Tested in Opera, Chrome, FF, and IE.IE was the only one with problems.
Scotta
I know it is, but it only relies on Javascript on the one browser that can't do it without it. Mine works in everything except IE6 whether or not Javascript is enabled (and it's easier to work with too, since once the script it set up, you can just write normal CSS and it will work).
Brendan Long