Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move the cursor into a input text box by clicking on html button?

Hi Can anybody tell me How to move the cursor into a input text box by clicking on html button ?

like image 778
user3049033 Avatar asked Mar 04 '14 13:03

user3049033


3 Answers

Try this:

document.getElementById("textbox").focus();
like image 91
Rokin Avatar answered Oct 01 '22 02:10

Rokin


HTML:

<input type="text" id="my_textbox" value="My Text" />
<button id="my_button">Focus</button>

JS:

document.getElementById('my_button').onclick = function() {
    document.getElementById('my_textbox').focus();
};

Example:

http://jsfiddle.net/GeoForce/ZnVH7/

like image 39
Geo Avatar answered Oct 01 '22 03:10

Geo


You can just do it by using jQuery as follows:

Include jquery in HTML HEAD section then

$( "#button" ).click(function() {
$( "#targetinput" ).focus();
});

Or without jquery you can do it using

getElementByID("targetinput").focus();
like image 25
عثمان غني Avatar answered Oct 01 '22 03:10

عثمان غني