469,307 Members | 2,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Editing DIVs with classname JQuery

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

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.    var js = "alert(this.className); return false;";
  3.    var newclick = new Function(js);
  4.    $("div").attr('onclick', '').click(newclick);
  5. });
  6.  
This code works fine but the problem is that I only need DIV tags which have a class attribute.

for Example :

Expand|Select|Wrap|Line Numbers
  1. <div class='test1'></div>
  2. <div></div>
  3. <div class='test2'></div>
  4.  
In the above code only first and third DIVs should be selected

Thanks / Behzad
Sep 5 '11 #1

✓ answered by Dormilich

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)

2 1576
Dormilich
8,651 Expert Mod 8TB
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)
Sep 6 '11 #2
bnashenas1984
258 100+
Thank you Dormilich
Your code did the magic :)
Sep 6 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by bridgemanusa | last post: by
2 posts views Thread by samuelberthelot | last post: by
4 posts views Thread by Steffan A. Cline | last post: by
53 posts views Thread by souporpower | last post: by
3 posts views Thread by mberkom | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.