rkevx21
rkevx21

Reputation: 3999

Javascript: Tab Index - Focus

I need some help. It is all about tab index. I have a javascript that uses document.getElementById("name").focus(); where the textbox_1 with the id="name" is focus. The problem is I uses a reload page that when I input a text in another textbox_2 with id="test" the page will reload and after reloading the tab index will be in the textbox_1, I want to do have an output that it will be stay at the textbox_2 after reloading.

Upvotes: 0

Views: 515

Answers (1)

MartinWebb
MartinWebb

Reputation: 2008

This demonstrates how to save focus in localstorage when page is reloaded or re-visited.

http://jsfiddle.net/3w3h2z28/

 <input id="text1" type="text" onfocus="savefocus(this)">
 <input id="text2" type="text" onfocus="savefocus(this)">


var ls=window['localStorage'], 
    focus=ls.getItem("onfocus") || "text1",
    savefocus=function(e){ls.setItem("onfocus", e.id)};

    document.getElementById(focus).focus();

Upvotes: 1

Related Questions