0

I have made a chrome extension, and I added a option page for it. I had a input on the option page like this <input id="inpID"> and did call a external javascript file like this <script type="text/javascript" src="whatever.js"></script>

The whatever.js is like this:

var inpID = document.getElementById('inpID');
...
function getData(key){
    var tem = '';
    chrome.storage.local.get(key,function (msg){
        tem = JSON.stringify(msg).substr((key.length+5),JSON.stringify(msg).length-(key.length+7));
    })
    return tem;
}
function setData(key,value){
    chrome.storage.local.set({key: value},function(){});
}
...
inpID.addEventListener('keyup',saveID);
...
function saveID() {
    setData('ID',inpID.value);
}
...
inpID.value = getData('ID')

However, this piece of code did not work at all. I type something in the input, refresh page, the input is still blank. Any helps?

David Xu
  • 169
  • 1
  • 3
  • 14

1 Answers1

0

use Jquery.

$("#ID").attr("value",ID)

as an example, this works on the stackoverflow-site:

$("#search input").attr("value","myValue")

Maybe your problem lies somewhere else, since your code should work as it is.

As a test, you might want to set the background-color of inpID and echo getData('ID') to console.

wotanii
  • 2,470
  • 20
  • 38
  • No but what I really want is to automatically save what user has typed into the storage. So that when the user reopen the page, it fetches the data from the storage and put it back on to the input element so that user's data gets saved(As I said, this is a option page. I will have to use the data in a pop up windows later) @wotanii – David Xu Dec 11 '15 at 05:35
  • Enter some data in your input field. Then set a breakpoint on the last line. is there a value for `getData('ID')` and is there a value for `inpID.value`? If you can't stet a breakpoint, user alerts. – wotanii Dec 11 '15 at 05:38