Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
How to Save Datalist Permantly with Appended Option

I am writing a website, one section of the website is for users to either select an existing TesterName or enter a new one.

var curTester;

function SetTesterName() {
  var Value = document.getElementById("testerNameInput").value;

  if (!Value) return;

  if ( $("#testerNameList").find("option[value='" + Value + "']").length == 0 ) {
    var option = document.createElement("option");
    option.value = Value;
    document.getElementById('testerNameList').appendChild(option);
    alert('New tester name added');
  }

  curTester = Value;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label for="testerNameInput" >Tester Name</label>         
<input id = "testerNameInput" list = "testerNameList" name = "testerNameInput" type = "text" style="width: 70%"/>
<datalist id="testerNameList">
  <option value="Name1">
  <option value="Name2">
  <option value="Name3">
</datalist>

<button onclick="SetTesterName()">Submit</button>

This 2 pieces of code work fine, but when I reload the webpage and look into the datalist, the new appended option is missing. What should I do?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

if you want to solve only frontend you can use localStorage for this problem.

If you want users to see what has been added by other users, you need a backend to handle database operations.

i edit your snippet. you can check here

var curTester;
var testerNameList = localStorage.getItem('testerNameList');

function SetTesterName() {
    var Value = $('#testerNameInput').val()

    if (!Value) return;

    if ( $("#testerNameList").find("option[value='" + Value + "']").length == 0 ) {
        // var option = document.createElement("option");
        // option.value = Value;
        // document.getElementById('testerNameList').appendChild(option);
        // you can use short way like below
        $('#testerNameList').append($(`<option value="` + Value + `"></option>`))
        testerNameList = localStorage.getItem('testerNameList');
        if(!testerNameList){ // localStorage empty
            localStorage.setItem('testerNameList', JSON.stringify([Value]));
        }else{
            let arr = JSON.parse(testerNameList)
            arr.push(Value)
            console.log(arr)
            localStorage.setItem('testerNameList', JSON.stringify(arr));
        }
        alert('New tester name added');
    }

    curTester = Value;
}

// When the page is ready it adds the elements found in the local storage.
// Shorthand for $( document ).ready()
$(function() {
    if(testerNameList != null){
        JSON.parse(testerNameList).forEach(x => {
            $('#testerNameList').append($(`<option value="` + x + `"></option>`))
        })
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!