I have a series of input
elements each with a few <div>
tags separate.
I like to have if where I can click
on any of the <div>
and it will focus
on an input
.
Is this possible and if so can anyone give ideas on script?
6 Answers
I don't see a reason why you need JS
to do this when such feature is already provided in HTML
.
<label for="YOURID">The clickable region<label>
<input id="YOURID" type="text" />
-
1
-
2
Try this with jquery:
$('#yourdiv').click(function() {
$('#yourfield').focus();
});
Try this :
<input id="myInput" />
<div onclick="document.getElementById('myInput').focus(); return false;"></div>
-
1
$('div').click(function() {
$(this).find('input[type="text"]').focus();
});
-
1Like this one over the other solutions as this is more generic and uses unobtrusive JS.– apnerveApr 15, 2012 at 17:03
-
- using javascript (call .focus() on the input element)
- using wrapped around your div (makes only sense if the div is the label)
USING HTML ("for") :
HTML provides this functionality. Using "for" "id" you can easily achieve it.
<label for="idname">Click Here to Reach Input<label> <input id="idname" type="text">
- USING jQuery ("focus")
$(".classname").click(function(){
$("input").focus(); })
labels
? I think you shouldn't rely on JavaScript for simple stuff like this when there are dedicated HTML tags for that purpose.