Class created during document loaded

Hi, I have an element (‘a’) which is no class and no Id, dynamically i created the class during the document loaded.

this is how i add class on a element.


I putted an alert so that i will know that i click the firstclass.

    alert("this is firstclass");


unfortunately it will not alert.but i inspect in the firebug the firstclass is in there…I don’t know why it did not alert.

please enlighten my mind on this,.

Thank you in advance.

Because there is an error in your JS.


should be:


Note the missing dot before the class name.

yup,i got wrong typo for that,but here in my code


still does not work.

Hi pullo, can i ask something what is the event.preventDefault() ? what is the purpose for this?

Hi there,

I think that your problem is that you are not stopping the browser from following the link and that is why you are not seeing an alert.
This is what event.preventDefault() or return false are for.
See this:

Other than that, this will work as expected:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jQuery addClass</title>
    <script src="" type="text/javascript"></script>

    <div class="mainclass">
      <a href="">Sitepoint</a><br />
      <a href="">Google</a>
      $(document).ready(function() {
          alert("this is firstclass");
          return false;

Okay,it show now the alert.THank you :slight_smile: