jQuery: trigger click or focus input field
102,100
Solution 1
I guess you want:
$(".triggerBtn").click(function () {
$(this).closest('div').find('.inputField').focus();
});
Solution 2
add Onclick="function()" see here if you need to trigger it manually using jquery you can to this by
$("#field1").trigger("click");
Solution 3
$(".triggerBtn").on("click",function(e){
$(this).closest("div").find(".inputField").click();
//or $(this).closest("div").find(".inputField").focus();
});
Author by
user2571510
Updated on April 12, 2020Comments
-
user2571510 about 4 years
I have a page with multiple divs that all look like the example below. Each div contains a field, a hidden field and a button.
How can I achieve that by click on the button the (visible) input field gets triggered ? I need to trigger either a click or focus as both fire the same function.
Each button in question has the
class="triggerBtn"
and the corresponding input field has theclass="inputField"
.Example div:
<div> <input type="text" class="inputField" id="field1" name="field1" /> <input type="hidden" name="field1" /> <button type="button" class="btn btn-primary triggerBtn">Find</button> </div>
-
TCHdvlp over 10 yearsHow can you focus an array of elements ?
-
TCHdvlp over 10 yearsYou guess... Like all of us. As long as this question is unclear, we can't help him/her.
-
Adam Merrifield over 10 yearsshouldn't
input[type:text]
beinput[type='text']
? -
user2571510 over 10 yearsYou guessed right ! :) Thanks for the quick reply and sorry I wasnt clear enough.