Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  83] [ 1]  / answers: 1 / hits: 48148  / 12 Years ago, sun, september 16, 2012, 12:00:00

I want to focus an input element when a div is clicked.



My HTML looks like this:



<div class=placeholder_input>
<input type=text id=username maxlength=100 />
<div class=placeholder_container>
<div class=placeholder>username</div>
</div>
</div>


And my script is:



$(#username).focus(function() {
$(this).next().hide();
});

$(.placeholder_input).mousedown(function() {
$(this).children(:first).focus();
});


When I click into the textbox, the placeholder text disappears correctly, but the blinking cursor doesn't show in the textbox. (and I can't type any text into the textbox)



Inside of the mousedown event handler, the $(this).children(:first) expression selects the correct input element, so I have no idea why the focus() call doesn't work.


More From » html

 Answers
12

It doesn't work with the mousedown method; it does, though, work with the mouseup() and click() methods:



$(.placeholder_input).mouseup(function() {              
$(this).children(:first).focus();
});​


JS Fiddle demo.



And:



$(.placeholder_input).click(function() {              
$(this).children(:first).focus();
});​


JS Fiddle demo.



References:




[#83068] Thursday, September 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shylaelisan

Total Points: 37
Total Questions: 94
Total Answers: 110

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;