views:

264

answers:

3

I'm trying to remove contents of particular div which are basically list items and a heading by using jquery empty so that I could replace with new contents. What happens when I run the code is, the whole div element blinked and flash the replaced content and then the old one reappear. Can anyone tell me what am I doing wrong? Here's an excerpt of my code -

$("#msg_tab").bind("click",function(){
  $("#sidebar1").remove(); 
  var html=".....";
  $("#sidebar1").append(html);
 });



<div id="sidebar1" class="sidebar">
   <ul>
    <li>
     <h2>Messages</h2>
     <ul>
      <li><a href="#">Compose New Message</a></li>
      <li><a href="#">Inbox</a></li>
      <li><a href="#">Outbox</a></li>      
      <li><a href="#">Unread</a></li>
      <li><a href="#">Archive</a></li>
     </ul>
    </li>
   </ul>
  </div>

Another question is, how do I write multiple line html code string in javascript so that java would recognize as a string value? Placing forward slash at the end is ok when the string is not a html code but, in html code, I can't figure out how to escape forward slash from ending tags.I've tried escaping it with backward slash but doesn't work. I would be appreciated if anyone could shed a light on this matter as well.

+3  A: 

You're using the wrong function. You want empty():

$("#sidebar1").empty();

remove() will remove the element entirely.

Alternatively you can use html() to replace all the contents.

Your key problem however is you don't stop event propagation. Your link is:

<a id="msg_tab" href="" class="tab msgtab">MESSAGES</a>

No href means if you click on such a link you will navigate back tot he same page. Your event handler should be:

$("#msg_tab").bind("click",function(){
  var html=".....";
  $("#sidebar1").html(html);
  return false;
});

The return false is to stop event propagation, which is currently forcing a page refresh. That's why you're not seeing the update.

cletus
I did as you suggested but the result was basically the same. The new content just flashed for a sec and the old one reappear. :confused
Andrew
Are you sure what you've posted is the *only* code you have?
Dean Harding
nope. There is another code in the very same function which doesn't relate to this element. Anyway, I'll post the the whole page for you to see - http://pastebin.com/1N82qiXL
Andrew
@Andrew I took a closer look at your code. See the update. I believe I've solved your problem.
cletus
you bet you did it :). I had a similar problem before when I used anchor as a button. Totally forget of it. Thanks so much.
Andrew
A: 

U can also use

$("#sidebar1").html("");

to empty the content of any particular div...

SpikETidE
+2  A: 

You're adding a click handler to a hyperlink that points to your current page.
After the click handler executes, the hyperlink gets clicked and the page reloads, resetting it to its original state.

You need to add return false; to your click handler to prevent the page from reloading.
Alternatively, you could change the href attribute to href="#".

SLaks
Yeap, that solves the problem. Thanks so much guys.
Andrew