Click on a h3 and focus on a form field

2

I am organizing a contact form, broken down into several parts.

One of the things I need would be that when the user clicks an h3 (but can also be a div, anchor, or button) a specific field of this form gets focused.

How could I accomplish this?

    
asked by anonymous 05.08.2016 / 15:40

2 answers

7

To use a h3 you can use an anchor associated with the ID of input .

It would look like this:

<h3><a href="#a">Clica-me para o input A</a></h3>
<h3><a href="#b">Clica-me para o input B</a></h3>

<input type="text" id="a">
<input type="text" id="b">

or as @ricardo also suggested use label like this:

<label for="a">
    <h3>Clica-me para o input A</h3>
</label>
<label for="b">
    <h3>Clica-me para o input B</h3>
</label>

<input type="text" id="a">
<input type="text" id="b">

And in these cases it makes no difference where on the page the elements are since each ID is unique (not repeated inside the page).

    
05.08.2016 / 15:45
3

There are a few ways you can solve your problem. The simplest would be to use elements HTML , like this:

<label for="minha_input"><h3>Input</h3></label>
<input type="text" id="minha_input">

If you do not want to, an alternative is to do via Java Script , like this:

function myFocus() {
  document.getElementById('minha_input').focus();
}
 <h3 onclick="myFocus()">Input</h3>
<input type="text" id="minha_input">

I hope I have helped.

    
05.08.2016 / 15:42