LightningBoltϟ
LightningBoltϟ

Reputation: 1373

How can I use a link to navigate between input boxes?

How can I use a link to navigate between input boxes? So if I click the link it will focus on a specific input box. The solution can be with PHP, CSS, JavaScript or HTML.

So:

<a href='link1' class='link'>

Will navigate to/focus on

<input type='text' id='input1' class='input'>

And

<a href='link2' class='link'>

Will navigate to/focus on

<input type='text' id='input2' class='input'>

P.S: I may also use jQuery

Upvotes: 1

Views: 130

Answers (4)

jogesh_pi
jogesh_pi

Reputation: 9782

Use this:

<a href='link1' class='link' onclick="document.getElementById('input1').focus(); return false;">link1</a>
<input type='text' id='input1' class='input'>

Define the onclick event for every link, with the input box id, according to example

http://jsfiddle.net/jogesh_pi/xWXg6/

Alternative - with JQuery:

If you have link and input with the same classes and id like this :

<a href='link1' class='link'>link1</a>
<input type='text' id='input1' class='input'>

<a href='link2' class='link'>link2</a>
<input type='text' id='input2' class='input'>

then use JQuery:

$('.link').each(function(i, e){
    $(this).click(function(ev){
        ev.preventDefault();
        $('#input' + (i+1)).focus();
    });
});

Demo2: http://jsfiddle.net/jogesh_pi/zNqtB/

Upvotes: 4

Anand Jha
Anand Jha

Reputation: 10724

Try this,

$( ".link" ).click(function() {
  $(this).next('input[type="text"]').focus();
});

Working Demo

Upvotes: 0

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

You can use <label> for this.

If you want to be done in jQuery.

$( ".link" ).click(function() {
    $('#input1').get(0).focus();  
});

Upvotes: 1

karaxuna
karaxuna

Reputation: 26940

You can use label for this purpose:

<label for="input1"> input1 label </label>
<input type="text" id="input1">

Fiddle

Upvotes: 0

Related Questions