skip to Main Content

I have a login page and I am storing the localStorage key’s value i.e. username taken from login page to a variable using var unameVar=localStorage.getItem('uname'). Now I want to assign the value of unameVar to a button. To put it short the button’s name should be that of unameVar on page load. Need help on this

javascript

<script>
    var unameVar=localStorage.getItem('uname');;
    console.log(unameVar)
</script>

html

<button class="btn btn-link" > This value needs to be changed                    
</button>

2

Answers


  1. Just call onload function and pass the new value from localStorage.

    let unameVar=localStorage.getItem('uname');
    let button = document.querySelectr('button');
    
    /*When Page is loaded completely*/
    window.onload = function(){
    
       button.innerText = unameVar; //=====> Assing your localstorge value to button
    }
    /*-----------------------------*/
    
    Login or Signup to reply.
  2. Add an id to the button then after retrieving value from localStorgae, use text from jquery library to add text

    $('#buttonID').text(unameVar.trim())
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search