user3344186
user3344186

Reputation: 191

How to change focus on textbox dynamically?

I'm working on a script that interacts with an html page.

The html page has two text boxes, and a submit button.

Each time you press the send button the focus on the textbox moves from the first to the second and so on.

Until now I couldn't onFocus to interact with the code. Who can help me figure out how to fix it?

I found this: http://www.w3schools.com/jsref/event_onfocus.asp

This is my html code:

<html>
<input type="text" id="c1"><br>
<input type="text" id="c2"><br>
<input type="button" id="enter" onClick="focus();">

<script>
function focus()
{

}
<script>
</html>

Upvotes: 1

Views: 1746

Answers (2)

Daniel
Daniel

Reputation: 3514

You need to use the focus() method for the element you want to be focused:

function focus()
{
    document.activeElement.nextSibling.focus();
}

that should move the focus to the next dom element each time you click the button.

Edit:

You could write something like this:

function focus()
{
    if (document.activeElement.getAttribute('id') == 'c1'){
        document.getElementById('c2').focus();
    }else {
        document.getElementById('c1').focus();
    }

}

Upvotes: 2

user3473776
user3473776

Reputation: 62

Basically all u want to do is check to see if the form has been filled in? If not it focuses on a particular field which has not been filled in?

Upvotes: 0

Related Questions