Zac
Zac

Reputation: 121

Input value doesn't display in field

I have this function:

function stringGen(len){
    var text = " ";
    var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
    for( var i=0; i < len; i++ )
        text += charset.charAt(Math.floor(Math.random() * charset.length));
    return text;
}
var commande = stringGen(8);
document.getElementById("commande").innerHTML=commande;

And this input:

<input type="text" id="commande" name="commande" class="field" >

I would like to display the result of the "commande" variable but it simply doesn't display and i don't know why. Is it because the value JS object and the value HTML object are not the same?

Upvotes: 0

Views: 3607

Answers (1)

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11707

use value instead of innerHTML

document.getElementById("commande").value=commande;

function stringGen(len) {
  var text = '';
  var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
  for (var i = 0; i < len; i++)
    text += charset.charAt(Math.floor(Math.random() * charset.length));
  return text;
}

document.addEventListener('DOMContentLoaded', function() {
  var commande = stringGen(8);
  document.getElementById("commande").value = commande;
}, false);
<input type="text" id="commande" name="commande" class="field">

Upvotes: 1

Related Questions