tags:

views:

45

answers:

3

I'm trying to add content to a div every time a link is clicked. However, the added text dissapears immediately after being added. What am I doing wrong?

 <html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="/files/jquery.js"></script>          
 <script type="text/javascript">                                         
     $(document).ready(function() {
      $("a").click(function() {
      $("#questions").prepend("A question<br />"); 
      });   
});

</script>                                                               
</head>                                                                 
<body>                                                                  
 <a href="">Add</a>
   <p id = "questions"> </div>
   </body>                                                                 
   </html>
+4  A: 

<a href="#">Add</a>

You need to have something in your "href", else it'll go navigate to the same page (at least for Firefox).

o.k.w
true, Firefox does that. IE navigates to the root of site / directory you are in.
Luke Duddridge
+5  A: 

Add return false; to your click-event, to prevent it from reloading the page.

Magnar
+2  A: 

I agree with O.K.W

also putting return false; or passing through a variable to represent the control on the function and performing preventDefault will stop the natural click event.

function(e){
e.preventDefault();
}
Luke Duddridge
+1 preventDefault() does a good job at expressing intent.
David Robbins