Search code examples
javascripthtmlsessionstorage

get javascript variable in html5


I have one html5 page and I store a variable in some javascript. I store the clicked item from a ul in a variable called "namer".

<script>
    $( document ).ready(function() {
       $( "a" ).click(function( event ) { 
           sessionStorage.setItem("namer", $(this).text());
           window.location="Antibiotics2.php";
       }); 
       event.preventDefault();
    });
</script>  

Now when the new window launches I want to access "namer" from the html5 code so that I can name a header after the retrieved value. I want to put the value in the code below where I wrote "namer".

<!DOCTYPE html>
<html>
    <head> 
    </head>
    <body>
        <div data-role="page" class="div1">            
          <div data-role="header"> 
        <h1>"namer"</h1> 
          </div>
          <div data-role="content">
          </div>
        </div>
    </body>
</html>

I can access it from javascript with the key no problem but can't figure out how to get it from html.


Solution

  • You already using sessionStorage.setItem to set session variable, now use getItem to get value back from session.

      $( document ).ready(function() {
          var namer = sessionStorage.getItem("namer");
          $('h1').text(namer)
      });