0

i am modifying a empty <div> with the .html() method of jQuery. When i refresh the page the added code disappears.

$('#div').html($('#div').html() + '<div>'+obj.name+'&nbsp;<a href="">(x)</a>');

Is it possible to change the behaviour or use other methods?

thanks

Edit:

to calify my problem

Maybe it is a design error. It is a form with some fields and a second form where the user can upload files, after a file is uploaded i add the filename and a delete link in a div. If the user submits the form the program connects the created object and the files. It is not really a page where anybody should need to refresh, but it can happen and then the files are saved in the database but the information is lost to connect it with the form object.

4
  • 3
    We need to know when that modification takes place. On click? if yes, how can you expect that on page refresh that click will be called - if you don't call it your self, or you just plain-call a function? May 5, 2012 at 15:55
  • +1 Roko, this is totally context dependent. What refreshes the page? When is the modification made? How? Any other frameworks involved? May 5, 2012 at 15:58
  • I don't get it, are you replacing the entire html with the same html + an element or two?
    – adeneo
    May 5, 2012 at 15:58
  • Maybe it is a design error. It is a form with some field and a second form where the user can upload files, after a file is uploaded i add the filename and a delete link in a div. If the user submits the form the program connects the created object and the files. It is not really a page where anybody should need to refresh, but it can happen and then the files are saved in the database but the information is lost to connect it with the form object.
    – KenavR
    May 5, 2012 at 16:04

3 Answers 3

3

Of course, whenever you are reloading the page, all the elements will go back to the original state (as loaded from the server). However there are ways of solving this.

  • Using cookies
  • Using HTML local storage

You have to understand how the client side scripting works. The modification happens on the client's system, not on the server, from where the script is going to load.

2

you should create a function:

function doMyStuff(){    
     //your code here    
}
doMyStuff();   // that will be triggered on DOM ready // page refresh, whateva'

$('#element').on('click',function(){
    doMyStuff(); // you call your function on click
});
0

I don't really understand what you're doing here, but you could put the link you want in a #div p a on the HTML page itself. Then if you want to change that line of code when the user clicks a button, you could do that using CSS.

Specifically, like this:

<div>
  <p><a id="example" href="www.example.com">Example</a></p>
</div>

and if say, the user clicks a button (onClick()...)

$('div p a#example').html(//what you want to do here);

That should probably change the code.

That way, your browser doesn't display an empty spot in the HTML itself whilst you wait for the client to make a move first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.