Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value on page load using javascript?

I have to set dynamically text value when page was load. The code is..

$(document).ready(function() {
    //Function call
    setname();
});

function setname() {
  document.getElementById('loginname').value = localStorage.getItem("username");
}
<span class="text-muted text-xs block" id="loginname"></span> 

I get value from my local session but why they are not set on my loginname field? I don't know why, please help me.

like image 532
piyush Avatar asked Jun 01 '15 08:06

piyush


1 Answers

span elements do not have a value property - you need to set innerText:

function setname() {
    document.getElementById('loginname').innerText = localStorage.getItem("username");
}

Alternatively, you can use jQuery exclusively:

$(function() {
    $('#loginname').text(localStorage.getItem('username'));
});
like image 137
Rory McCrossan Avatar answered Sep 30 '22 05:09

Rory McCrossan