Editing DIVs with classname JQuery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bnashenas1984
    Contributor
    • Sep 2007
    • 257

    Editing DIVs with classname JQuery

    Hi everyone
    I'm using JQuery to add an onClick event to all DIV tags on my HTML page

    Here's the code:
    Code:
    $(document).ready(function(){
       var js = "alert(this.className); return false;";
       var newclick = new Function(js);
       $("div").attr('onclick', '').click(newclick);
    });
    This code works fine but the problem is that I only need DIV tags which have a class attribute.

    for Example :

    Code:
    <div class='test1'></div>
    <div></div>
    <div class='test2'></div>
    In the above code only first and third DIVs should be selected

    Thanks / Behzad
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    since jQuery uses CSS selectors you should be able to use $("div[class]") (or something like that, you would have to look up the correct syntax, if that fails)

    Comment

    • bnashenas1984
      Contributor
      • Sep 2007
      • 257

      #3
      Thank you Dormilich
      Your code did the magic :)

      Comment

      Working...