Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cursor to input box in Javascript?

document.getElementById(frmObj.id).value=""; document.getElementById(frmObj.id).autofocus; document.getElementById("errorMsg").innerHTML = "Only numeric value is allowed"; 

In the above code the value of the form object is perfectly setting to "" but there is no cursor in the text box. I want a cursor to be there. focus() only focuses that input box but does not actually set the cursor.

like image 226
yukti kamra Avatar asked Jun 05 '12 09:06

yukti kamra


People also ask

How do I set the input field cursor?

To move the cursor to the beginning of an input field:Use the setSelectionRange() method to move the cursor to the beginning of the input field. Call the focus() method on the input element. The focus method will move the cursor to the beginning of the element's value.

How do I add a cursor to a text box?

Sometimes all you have to do to make sure the cursor is inside the text box is: click on the text box and when a menu is displayed, click on "Format text box" then click on the "text box" tab and finally modify all four margins (left, right, upper and bottom) by arrowing down until "0" appear on each margin.

How can I move cursor from one textbox to another in jQuery?

You can use JavaScript's Onchange Event (or JQuery may be) on the Textbox which calls a method, where You can check the value of the Textbox if it equals the Maximum value , then setFocus on the next Textbox. Show activity on this post. Use the TextBox. Focus() method on the next TextBox.

How do you focus an input box in HTML?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.


1 Answers

In JavaScript first focus on the control and then select the control to display the cursor on texbox...

document.getElementById(frmObj.id).focus(); document.getElementById(frmObj.id).select(); 

or by using jQuery

$("#textboxID").focus(); 
like image 83
Talha Avatar answered Nov 16 '22 02:11

Talha