1

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>

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

5

<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).

1 Comment

true, Firefox does that. IE navigates to the root of site / directory you are in.
2

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();
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.