1

I am trying to append a javascript variable within one of my html tags however it does not work at all even though the variable has a value. I am grabbing the variable through local storage so I am not sure if it is because of that or not? Any help would be greatly appreciated!

<div id="wrapper">
  <!-- Sidebar -->
  <div id="sidebar-wrapper">
    <ul class="sidebar-nav">
      <li class="sidebar-brand">
        <h3 id="welcome"></h3>=
        <script>
          var name = localStorage.getItem('name')
          document.getElementById("welcome").value = name;
        </script>
      </li>
      <li>
        <a href="#">Dashboard</a>
      </li>
      <li>
        <a href="#">Shortcuts</a>
      </li>
      <li>
        <a href="#">Overview</a>
      </li>
      <li>
        <a href="#">Events</a>
      </li>
      <li>
        <a href="#">About</a>
      </li>
      <li>
        <a href="#">Services</a>
      </li>
      <li>
        <a href="#">Contact</a>
      </li>
    </ul>
  </div>
</div>

10
  • You open a P tag and you close a h3 tag, this looks like a typo Commented Dec 6, 2018 at 11:56
  • Apologies, supposed to be a h3 tag Commented Dec 6, 2018 at 11:57
  • How is this value being stored in the localStorage? Commented Dec 6, 2018 at 11:58
  • Here you can find iformation how to view or edit your local storage: stackoverflow.com/questions/9404813/… Commented Dec 6, 2018 at 11:59
  • 1
    so use .textContent = name or .innerHTML = name... not .value = name. value are just for input like elements Commented Dec 6, 2018 at 12:03

2 Answers 2

0

if the name variable is definitely set use .innerHTML instead of .value

Sign up to request clarification or add additional context in comments.

2 Comments

That worked, thanks for the quick solution!
Thats no problem
0

Try innerHTML

The innerHTML property sets or returns the HTML content (inner HTML) of an element.

<script>
var name = localStorage.getItem('name')
document.getElementById("welcome").innerHTML= name;
</script>

1 Comment

Please add a bit of explanation. Just dropping code is discouraged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.