Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to pass value of Element between HTML pages as below

Page1

<!Doctype html>
<html>
<body style="margin:0;padding:0">


<div  style="margin-top:20px">
    
 <p id="pid" style="margin-bottom:0;float:right;display:inline;">1001</b</p>
 <button id="btn1" style="display:inline;float:right;background-color:#0000FF;color:white" onclick="testJS()"  >Buy Now</button>    
    
</div>

<script>

    function testJS(){
     
 
        var favoritemovie = document.getElementById('pid').value;
        sessionStorage.setItem("favoriteMovie", favoritemovie);
        window.location="buy.html";    
    }
    
    
   
   
</script>  



</body>
</html>


Page 2

<!Doctype html>
<html>
<body style="margin:0;padding:0">
<div>    
    <form action="#" method="post">
        
            <div >
                <p id="tpid"></p>
            </div>
     
    </form>
    
    
</div>

<script>

        
       function myFunc(){
                var favoritemovie = sessionStorage.getItem("favoriteMovie");
                document.getElementById('tpid').innerHTML = favoritemovie;

       }
       window.onload=myFunc;
          
</script>
    


</body>
</html>



Value shows Undefine instead of "1001" why?.
please suggest where am I wrong?.

What I have tried:

I have invest my whole day on it but not success.
Posted
Updated 12-Feb-23 6:59am

1 solution

text tags don't have a value, to get their content you use the innerHTML property:
var favoritemovie = document.getElementById('pid').innerHTML
 
Share this answer
 
v2
Comments
MAHESH WAGHELA 12-Feb-23 13:25pm    
Thanks For Solution, its works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900