Reputation: 554
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.
Upvotes: 5
Views: 8308
Reputation: 72299
Since it is span
elements so you can not have value
property for it.you need to set innerText
:
function setname() {
document.getElementById('loginname').innerText = localStorage.getItem("username");
}
You can do it through innerHTML
also like this:-
function setname() {
document.getElementById('loginname').innerHTML = localStorage.getItem("username");
}
Upvotes: 4
Reputation: 2465
Try using the jquery operators to set the value:
EDIT: appearantly span doesn't have the val attribute instead you need to set the text attribute, edited in solution
$(document).ready(function() {
//Function call
setname();
});
function setname() {
$('#loginname').text(localStorage.getItem("username"));
}
Upvotes: 4
Reputation: 337580
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'));
});
Upvotes: 6