Home > Software design >  Focus an element given an html input tag? Jquery
Focus an element given an html input tag? Jquery

Time:09-22

I have an html tag "tabbedCell" returned which can be viewed below:

<input type="text" data-row-id="12630" class="edit numeric ui-draggable ui-draggable-handle" data-field="May" value="135" style="position: relative; cursor: cell;">

I'm trying to set focus on this element, however I have confirmed the focused element is undefined after setting focus. Any suggestions?

EDIT: There are multiple inputs on my page so the answers would not work. I need to focus this specific input element if multiple exist.

console.log(tabbedCell)
$(tabbedCell).focus();
console.log(($(':focus')[0]));

CodePudding user response:

Add id="sample_input"

<input type="text" id="sample_input" data-row-id="12630" class="edit numeric ui-draggable ui-draggable-handle" data-field="May" value="135" style="position: relative; cursor: cell;">

This is how you focus
# -> Means id

$("#sample_input").focus();

Edit: Since you dont want to add another id use this:

$("input[data-row-id=12630]").focus();

Edit 2: like this

$("input[data-row-id=12630][data-field=May]").focus();

CodePudding user response:

Use an ID or Class of the element instead, like here you can use "edit".

$(".edit").focus();
  • Related