简体   繁体   中英

How to save the values from a textbox and dropdown in localStorage?

I want to save the values entered in a textbox and also the one selected from a drop-down into localstorage. I tried the following code:

<table class="dynatable" id="my">
    <tr>
        <td><input type="text" id="name" name="name1" onchange="saveSelectedValues();"></td>
        <td><select id="type" onchange="saveSelectedValues();"><option>Text</option><option>Paragraph</option><option>Dropdown</option></select>
    </tr>
    <tr>
        <td><input type="button" id="add" value="Add" onclick="Javascript:addRow()"></td>
    </tr>
</table>

and my saveSelectedValues function is as below:

function saveSelectedValues(){
    var myName = document.getElementById("name").value;
    var type = document.getElementById("type").value;

    localStorage.setItem("attributeName",myName);
    localStorage.setItem("attributeType",type); 
}

But the values are not getting saved in localstorage.

Your code works fine

saveSelectedValues = function(){
    var myName = document.getElementById("name").value;
    var type = document.getElementById("type").value;

    localStorage.setItem("attributeName",myName);
    localStorage.setItem("attributeType",type); 
     alert(localStorage.getItem("attributeName"));
}

look for below jsfiddle

jsfiddle

Make sure your browser is supporting html 5 local storage functionality and setting to allowed local storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM