2

Sorry If my question is duplicate, I couldn't find anything similar. I'm trying to send my values from one HTML to another HTML using local storage, my HTML1 page contains grocery items when I select and click next button the items are stored in a final variable it has to be sent to another HTML2 page i.e. the billing session

//Htmlpage1 want to send the radio button details to html2page
        <form class="mt-5"> 
            <div class="row mt-5">
                <div class="col-sm-4">
                  <bold><p>Black Rice</p></bold>
                    <input type="radio" name='item1' value='1 KG @ Rs.199' data-id="blackrice1kg" />1 KG @ Rs.199<br>
                    <input type="radio" name='item1' value='2 KG @ Rs.390' data-id="blackrice2kg" />2 KG @ Rs.390<br>
                    <input type="radio" name='item1' value='3 KG @ Rs.550' data-id="blackrice3kg" />3 KG @ Rs.550<br>
                </div>
                <div class="col-sm-4">
                        <img src="img/Taruproduct.png" alt="">
                    <bold><p>JaggeryPowder</p></bold>
                    <input type="radio" name='item1' value='100 Sachets @ Rs.199' data-id="JaggeryPowder1kg" />100 Sachets @ Rs.199<br>
                    <input type="radio" name='item1' value='200 Sachets @ Rs.390' data-id="JaggeryPowder2kg" />200 Sachets @ Rs.390<br>
                    <input type="radio" name='item1' value='300 Sachets @ Rs.550' data-id="JaggeryPowder3kg" />300 Sachets @ Rs.550<br>
                </div>
 //html2 page 
    <div class="container text-center">
           <h5 class="display-2 mt-5 mb-5">Subscription Box</h5>  
           <form>
           <div class="form-group">
           <p>Black Rice <input type="text" id="blackriceqty"  class="form- 
             control"></p>   
           <form>     
    </div>
//javascript code
 $(document).ready(function () {
   $("input[type='radio']").click(function(){
      var final = $("input[name='item1']:checked").val();
      $(".result").val(final);
      localStorage.setItem(textValue,final);
      document.getElementById("blackriceqty").innerHTML=localStorage.getItem(textValue);
     });
nick zoum
  • 7,216
  • 7
  • 36
  • 80

1 Answers1

1

Your JavaScript code has to be on the 2 different pages. On the first page you put the value in the local storage. On the second page you extract the value from the local storage. What you are trying to do in your example is to access the element with id blackriceqty with the JS code on the first page, but this element is on the second page - hence missing in the current DOM.

JavaScript code on 1st HTML page:

 $(document).ready(function () {
   $("input[type='radio']").click(function(){
      var final = $("input[name='item1']:checked").val();
      $(".result").val(final);
      localStorage.setItem(textValue,final);
     });

JavaScript code on 2nd HTML page:

$(document).ready(function () {
document.getElementById("blackriceqty").innerHTML=localStorage.getItem(textValue);
});

There are also other ways to pass values between pages as other comments says: go to the backend, use query strings, cookies, local or session storage, web sockets...

antanta
  • 618
  • 8
  • 16