Reputation: 1373
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
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/
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
Reputation: 10724
Try this,
$( ".link" ).click(function() {
$(this).next('input[type="text"]').focus();
});
Upvotes: 0
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