Reputation: 1945
I want to dynamically focus on input element at run time. I will pass input name runtime and then it should focus the input accordingly.
This is code for input element
<input type="text" id="txtNameOfEmp" data-bind="event: { blur: ValidateName.bind($data,txtNameOfEmp) } " />
Then in my view model i am trying to focus as below
self.ValidateName = function (val) {
var inputName = val.id;
// some logic for validation
alert("Name is not valid");
$("'#" + inputName + "'").focus()
};
I get error
JavaScript runtime error: Syntax error, unrecognized expression: '#txtNameOfEmp'
How to focus dynamically?
Update1
I have also tried below but it doesnt give error but doesnt set any focus as well
$(inputName).focus();
Upvotes: 2
Views: 131
Reputation: 56429
You've got too many quotes. You don't need any as you've wrapped your id in a variable.
Try this:
$("#" + inputName).focus()
Upvotes: 2
Reputation: 2770
Man... there is a mistake in your code as I see it:
remove the single quotes:
use $("#"+inputName).focus();
Upvotes: 1