2

I have a search text box after click in submit button textbox value is retain, but when i reload the page the textbox value still in a text box, how do i remove the value of text box after reloading/refreshing the page.

<script>
    $(document).ready(function(){
    document.getElementById("text1").value = localStorage.getItem("item1");
        });

   $(window).on('beforeunload', function() {
    localStorage.setItem("item1",document.getElementById("text1").value);
  });
</script>

Html.

<td>
<form action="/searchemp" method="post" >
    Selected Employee <input type="text"  name="EmployeeName" id="text1">
                     <input type ="submit" value="check">
</form> 

Give me a help or suggestion to fix it.

Thank you.

  • Why you can not delete the `value` from the `localStorage` ? – Kalpesh Rajai Apr 18 '16 at 09:26
  • @ Kalpesh Rajai, how could i delete...?? –  Apr 18 '16 at 10:19
  • `localStorage.clear();` @Rehan – Kalpesh Rajai Apr 18 '16 at 10:23
  • You are submitting the form to another page or same page ? – Kalpesh Rajai Apr 18 '16 at 10:24
  • If you Post the form to same page, So it is impossible to achieve that you want. Because when you are posting the page than it post the data to server and reload the page and in same way while reloading/ refreshing the page is reloaded.. it work same. – Kalpesh Rajai Apr 18 '16 at 10:29
  • @ Kalpesh Rajai , same page, i did this localStorage.clear();, but not working –  Apr 18 '16 at 10:30
  • @ Kalpesh Rajai, yes sir, so how could i do this..?? –  Apr 18 '16 at 10:34
  • @ Kalpesh Rajai, sir do you have any idea to do it, please help me, i am trying lots but still not able to do. –  Apr 18 '16 at 10:47
  • What you want to do actually ? @Rehan – Kalpesh Rajai Apr 18 '16 at 10:59
  • @ Kalpesh Rajai, after giving value in the textbox and click submit button, its gives value in a table in same jsp page, I want to keep that textbox value after clicking the submit button. –  Apr 18 '16 at 11:07
  • So you can also use the `jQuery Ajax` to implement this. First create the another `JSP` and called that page using `jQuery Ajax` and get the output from it and display it on the page. http://www.w3schools.com/jquery/ajax_ajax.asp and http://api.jquery.com/jquery.ajax/ – Kalpesh Rajai Apr 18 '16 at 11:10
  • As my internet experience all major website implementing this technique. – Kalpesh Rajai Apr 18 '16 at 11:12

3 Answers3

1
        $(document).ready(function () {
            $("#form1").submit(function () {
                window.localStorage['text1_val'] = $("input[name = 'text1']").val();
            });
            $(window).load(function () {
                $("input[name = 'text1']").val(window.localStorage['text1_val']);
                window.localStorage['text1_val'] = '';
            });
        });

  <form id="form1" method="post">
    <input type="text" name="text1">
    <input type="submit">
  </form>
user4617883
  • 1,277
  • 1
  • 11
  • 21
0

By default a textbox value is empty at page loading. I guess your problem is due to storing and fetching the value from the local storage. The code responsible for the text appearance

$(document).ready(function(){
    document.getElementById("text1").value = localStorage.getItem("item1");
});
0

Why don't you clear the localstorage?

$(window).unload(function() {
    window.localStorage.removeItem(item1);
});

Code is not tested, but this should give you an idea...

Jeroen Bellemans
  • 2,049
  • 2
  • 25
  • 42