rahulchawla
rahulchawla

Reputation: 296

Focus an element given an html input tag? Jquery

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]));

Upvotes: 0

Views: 62

Answers (2)

user16967562
user16967562

Reputation: 1199

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();

Upvotes: 1

Fc0001
Fc0001

Reputation: 118

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

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

Upvotes: 0

Related Questions