Append value to an input field in Javascript

15,515

You have to get the old value first, and then add to it

function update(i){
    var elem = document.getElementById('taginput');
    var old  = elem.value;
    elem.value = old + i.innerHTML;
}

or you could add it to with += directly

elem.value += i.innerHTML;
Share:
15,515
Ajmal Rasi
Author by

Ajmal Rasi

Updated on August 21, 2022

Comments

  • Ajmal Rasi
    Ajmal Rasi over 1 year

    Here i'm trying to append some text to an input field but everytime the old value gets replaced with new value.

    HTML

    <input type="text" id="taginput" class="input" name="tags">
    

    Javascript

    function update(i){
     document.getElementById('taginput').value = i.innerHTML;
      }
    

    no jquery please.