Mala
Mala

Reputation: 14803

Simple jQuery - Appending to body causes following code to not work

I am trying to do two things:

  1. Append a div to the body
  2. Make all clicks to links class editlink make a popup and not go to their href

Doing just #2 is fine:

$(document).ready(function(){
//  $(body).append("<div>Hello world</div>");
  $("a.editlink").click(function(event){
    alert("Javascript-endabled users should see this");
    event.preventDefault();
  });
});

But if I uncomment the code for #1 like below,

$(document).ready(function(){
  $(body).append("<div>Hello world</div>");
  $("a.editlink").click(function(event){
    alert("Javascript-endabled users should see this");
    event.preventDefault();
  });
});

the div appears as expected, but clicking editlink links no longer gives me a popup and navigates to the link's href.

What's going on?

Upvotes: 0

Views: 86

Answers (2)

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827276

You are missing the quotes for your body tag selector:

$('body').append("<div>Hello world</div>");

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281425

Did you mean:

$("body")

rather than:

$(body)

?

Can I recommend that you use Firebug to get decent error reporting? You'd have found this very quickly with Firebug.

Upvotes: 1

Related Questions