I want to transform a div composed of image + text (for translation) in a submit button. Is it possible?
I managed to use the image as submit but the text is not a link and it gives a 'broken' feeling to the whole image + text
Don't use <div> tags to make clickable elements. Use <a> or <button> elements. This enables browsers with JavaScript disabled to interact with them as expected.
To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function. The function submits the form.
It is completely acceptable to use a DIV inside a <form> tag. If you look at the default CSS 2.1 stylesheet, div and p are both in the display: block category.
<div onclick="document.myform.submit()"></div>
Jquery usage.
$('div').click(function () {
$('form').submit();
});
You can do it in two ways.
div
For method 2 you can either do it through jQuery or without, With plain js
<script>
function submitOnClick(formName){
document.forms[formName].submit();
}
</script>
<form id="myForm" ...>
....
<div onclick="submitOnClick('myForm')>
...
</div>
...
</form>
Trevor's answer shows how to do it with jQuery.
In his example just replace the 'div'
and 'form'
with the ids of your div and form as this:
If ids of div and form are myDiv
and myForm
specify them as '#myDiv'
and '#myForm'
, here #
is used to specify the following string is an id of an element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With