Question

I want to display the field player but i can't:

<body>

       <header>
    <h1>My memory Game</h1>
</header>

<div id="timer">
    Player Name: 
    <span id="player-name">

               <script type='text/javascript'>

                sessionStorage.getItem('player');

                //alert("Your name is: " + sessionStorage.getItem('player'));    

               </script>    
    </span>
</div>

When i test it by using javascript alert everything work : the player name is desplayed without a problem Any idea please

Était-ce utile?

La solution

Use this:

document.getElementById('player-name').innerHTML = 
     sessionStorage.getItem('player');

Fixed code:

<body>
    <header>
        <h1>My memory Game</h1>
    </header>
    <div id="timer">
        Player Name: 
        <span id="player-name">
        </span>
    </div>
    <script type='text/javascript'>
        document.getElementById('player-name').innerHTML = sessionStorage.getItem('player');
    </script>    

You should do some javascript tutorials. ​

Autres conseils

sessionStorage.getItem('player') returns a string. you should do something with it. like display in alert. - which is working according to your details...

once you have the value you can put it in element

ie:

myelement.inneHtml=sessionStorage.getItem('player')

this will write it into your span you should place the script tag below the span (i do not know what happens if you overwrite it while execution, might work as well)

document.getElementById("player-name").innerHTML = sessionStorage.getItem('player');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top