1

I'm writting my first jQuery script which should render a list of anchors in a div (whose id is #content) when I click on another anchor (#dwLink). I get are the links displayed as I want, but just for milliseconds! then they dissapear.

This is the script I wrote:

$(document).ready(function() {
 $('#dwLink').bind('click', function() {
 $('#content').html(
 "<ul>" +
 "<li><a href=\"#\">Link 1</a></li>" +
 "<li><a href=\"#\">Link 2</a></li>" +
 "<li><a href=\"#\">Link 3</a></li>" +
 "</ul>"
 );
 });
});

Does it have something wrong? I can't find where the problem is. I know I could do it by writting plain javascript since it is a basic dom manipulation problem, but I want to do it in a jQueryesque way. I hope you can help me. Thanks!

asked Aug 18, 2011 at 4:28
0

1 Answer 1

9
$(document).ready(function() {
 $('#dwLink').bind('click', function(e) {
 e.preventDefault(); // prevent the default behavior of the link
 $('#content').html(
 "<ul>" +
 "<li><a href=\"#\">Link 1</a></li>" +
 "<li><a href=\"#\">Link 2</a></li>" +
 "<li><a href=\"#\">Link 3</a></li>" +
 "</ul>"
 );
 });
});

here is the fiddle http://jsfiddle.net/45ShY/

answered Aug 18, 2011 at 4:31
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.