After performing validation on a radiobutton set I need the form focus to move to a form element.
I have created a function that works perfectly (validates the fields and activates div with an error message) but it does not move the focus to the indicated element.
Can someone point me to what I'm doing wrong?
Html code:
<div id="txtotronodo" style="display: none;">
<input type="text" id="reply6" name="txtotronodo" class="form-control col-sm-12" placeholder="Indique el nodo">
</div>
JQuery code:
$(document).ready(function() {
function validaNodo() {
var nodo = $("input[name=nodo]:radio:checked").length;
if (nodo !== 1) {
$("#nodo").css("display", "block");
$(document).on('focus', 'reply6');
$('reply6').focus();
event.preventDefault();
}
else {
$("#nodo").css("display", "none");
}
}
});
A greeting and thanks in advance.
The focus can only be given to elements that can have it. A
<div>
is not one of them,<input>
,<select>
,<a>
... yes they are.Example:
Reference: focus()