5

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.

6
  • 2
    Could you perhaps include your form? - And why is this tagged as PHP? Commented Jun 1, 2015 at 8:02
  • 2
    There's nothing obviously wrong with the code you've shown. Does the loginname element exist in the DOM on load? Is the username value set in localStorage? Commented Jun 1, 2015 at 8:02
  • <span class="text-muted text-xs block" id="loginname"> </span> Commented Jun 1, 2015 at 8:02
  • @piyush put this statement alert(localStorage.getItem("username")); in setname() see what you get. Commented Jun 1, 2015 at 8:03
  • 1
    document.getElementById('loginname').innerHTML= localStorage.getItem("username"); Commented Jun 1, 2015 at 8:03

3 Answers 3

6

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'));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rory McCrossan
4

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"));
}

4 Comments

could you guys please give me feedback as to why you downvote this? this is how I do it so is there something wrong with it?
It's been downvoted because the val() method is for use on elements which have a value property, which span do not.
The reason I down voted was/is because as stated in the other answers, spans doesn't have a value and should use innterText instead, secondly, the code OP used wasn't wrong as such, so changeing to jQuery wouldn't have solved the issue.
When I was writing this answer there were no other answers and I didn't know about spans not having val but it's been edited now :D
4

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.