I'd like to create an HTML form submit button with the value 'add tag', however, the web page is in Swedish, so I'd like to have a different button text.
That is, I want to have a button like
enter image description here
but I want to have my code like
if (request.getParameter(cmd).equals("add tag"))
tags.addTag( /*...*/ );
Is this possible? If so, how?
-
4I think the value should not matter. You should simply check for the presence of "add_tag" in your POST datagreg0ire– greg0ire2010年11月13日 08:51:03 +00:00Commented Nov 13, 2010 at 8:51
-
7I read this as "I want the text of the input not to be relevant to my server side processing" so that when you have several translations for a form you don't get one of several text values posted. It matters when your form has several submit buttons and you want to do if(request.getParameter("submit-type").equals("add-tag") ... to see which one was pressed.ijw– ijw2010年11月13日 09:03:26 +00:00Commented Nov 13, 2010 at 9:03
6 Answers 6
It's possible using the button element.
<button name="name" value="value" type="submit">Sök</button>
From the W3C page on button:
Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content.
2 Comments
Following the @greg0ire suggestion in comments:
<input type="submit" name="add_tag" value="Lägg till tag" />
In your server side, you'll do something like:
if (request.getParameter("add_tag") != null)
tags.addTag( /*...*/ );
(Since I don't know that language (java?), there may be syntax errors.)
I would prefer the <button> solution, but it doesn't work as expected on IE < 9.
Comments
There are plenty of answers here explaining what you could do (I use the different field name one) but the simple (and as-yet unstated) answer to your question is 'no' - you can't have a different text and value using just HTML.
Comments
I don't know if I got you right, but, as I understand, you could use an additional hidden field with the value "add tag" and let the button have the desired text.
If you handle "adding tag" via JScript:
<form ...>
<button onclick="...">any text you want</button>
</form>
Or above if handle via page reload
2 Comments
The horrible (but effective) hack I wound up using for something like this:
<style>
.addtag {
position: relative;
}
.addtag span {
position: absolute;
left: 2px;
pointer-events none;
}
.addtag input {
color: transparent;
width: 6em;
}
</style>
<span class=addtag>
<span>Lagg till Tag</span>
<input type=submit value=add_tag name=action>
</span>
It means that the text visible "on" the button is distinct from the invisible text that's in the button for dom purposes. It requires manually aligning the sizes, and might break tools that expect semantic html (or eye-candy around button pushes). But it works.