user1835504
user1835504

Reputation: 149

How can I link this html button to the input type "text" so that when I press enter from the input type, the button is activated?

Here is the html code for the form:

<form>
 Input Twitter ID: <input type="text" name="userid" id="userid">  

<button type="button" onClick="getStatuses();">Get recent tweets</button>  

</form> 

Currently the button activates getStatuses(). I want it so that when the user presses enter/return after inputting text, it also activates the button. The input is used in getStatuses() and is referenced by the id of the input.

Upvotes: 0

Views: 3516

Answers (1)

tymeJV
tymeJV

Reputation: 104785

You can use the onkeyup attribute and call a function:

JS:

function checkKey(e){
    var enterKey = 13;
    if (e.which == enterKey){
        getStatuses();
    }
}

HTML:

<input type="text" name="userid" id="userid" onkeyup="checkKey(event)">

Upvotes: 1

Related Questions