Does jquery remove return after page refresh?

Hi, I am using jQuery remove, and when the page is refreshed, it returns the deleted item

Can anyone understand why? I need this to stay hidden / removed from the page.

JQuery

<script> $(document).ready(function(){ $("#button").click(function(){ $("#div1").remove(); }); }); </script> 

Php

 while($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC) and ($cnt < $max)) { echo "<div id='div1'>"."".$dbRow["Name"]."</h4><br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' />"."<br><br>".$dbRow["Instructions"]." <form method='POST'><input type='hidden' name='MealID' value= '".$dbRow['MealID']."'> <input type='submit' name='submit' value='Complete' id='button' ></form></div>"; $cnt++; 
+5
source share
1 answer

Any operation performed using jQuery is valid only for the lifetime of the current page. After reloading the page, all these changes will be lost.

If you want the button to remain hidden, you need to store this information somewhere. You can make a request to the server and save it in preference, so the next time you reload your PHP code, you can hide the button. You can also use localStorage to hide:

 <script> $(document).ready(function(){ var div1Removed = localStorage.getItem('div1_removed'); if (div1Removed) { $("#div1").remove(); } else { $("#div1").show(); } $("#button").click(function(){ $("#div1").remove(); localStorage.setItem('div1_removed', 'true'); }); }); </script> 

In the PHP code, we add display: hidden to the div, so it is initially hidden. When the page loads, the JavaScript code checks to see if it should be visible or not. If so, that makes it visible. If it is not, then it completely removes it.

 while($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC) and ($cnt < $max)) { echo "<div id='div1' style='display: hidden;'>"."".$dbRow["Name"]."</h4><br>"."<br><img src=/".$dbRow['Picture']."' width='150' height='150' />"."<br><br>".$dbRow["Instructions"]." <form method='POST'><input type='hidden' name='MealID' value= '".$dbRow['MealID']."'> <input type='submit' name='submit' value='Complete' id='button' ></form></div>"; $cnt++; 
+1
source

Source: https://habr.com/ru/post/1276265/


All Articles