Home Ask Login Register

Developers Planet

Your answer is one click away!

Miller February 2016

regain html after emptying it using jquery?

I have an if/else loop inside my jQuery. I need to hide HTML on my text box inside my if portion and regain it at my else part. How can I do this using jQuery?

if (test == 1) {
    $(#id).html(""); //code to hide html
} else {
    //code to regain

code to hide html will works fine,but how to regain the code after emptying it?


gurvinder372 February 2016

you can't regain lost HTML unless you have saved it somewhere,

you can either show/hide the element


or you can store the values somewhere in a variable

var html = "";
if (test == 1) {
    html = $(#id).html(); 
    $(#id).html( "" ); 
} else {
    $(#id).html( html ); 

or set it to localstorage (if you browser supports it)

if (test == 1) {
    localStorage.setItem("savedHTML", $(#id).html()) ; 
    $(#id).html( "" ); 
} else {
    $(#id).html( localStorage.getItem( "savedHTML" ) ); 

ashfaq.p February 2016

You can assign the complete html inside your div to a variable.

var htmlContent = $(#id).html();

Then in your else, you can assign it back.


Post Status

Asked in February 2016
Viewed 1,559 times
Voted 14
Answered 2 times


Leave an answer

Quote of the day: live life