Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a div as a submit button

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

like image 650
Syl Avatar asked Sep 18 '11 17:09

Syl


People also ask

Can I use a div as a button?

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.

Can I use a tag as submit button?

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.

Can I put div into 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.


3 Answers

<div onclick="document.myform.submit()"></div>
like image 104
Will Avatar answered Oct 14 '22 19:10

Will


Jquery usage.

$('div').click(function () {
    $('form').submit();
});
like image 20
Trevor Avatar answered Oct 14 '22 20:10

Trevor


You can do it in two ways.

  1. You can use a button with type submit and use CSS to style it as a div
  2. Add an onClick on the div, and submit the form on the click event.

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.

like image 44
danishgoel Avatar answered Oct 14 '22 19:10

danishgoel