Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2011, 05:34 PM   PM User | #1
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 790
Thanks: 208
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Question jQuery: Clicking anywhere but on a particular A tag

Hi

I have a question in jQuery.

I have a webpage with lots of links, buttons, text etc. The page also has an <A class="mylink" HREF="#">Click here</a> tag.

What exactly I want is, if I click anywhere on the page, button, text or any other <A></A> tag on that page except the A tag that has the class="mylink", an alert() should popup.

I tried the following but I still see the alert when I click on the <A class="mylink" HREF="#">Click here</a> tag.


Code:
<script>

$(document).ready(function(){

   $(document).click(function(){
        alert(123);
  });


});

</script>
Any neat solution for this?


Thanks in advance.
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 11-08-2011, 06:15 PM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Just check the event's target:

PHP Code:
$(function () {
    $(
document).click(function (e) {
        if ( ! $(
e.target).hasClass('mylink')) {
            
alert(123);
        }
    });
}); 
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 11-09-2011, 02:21 AM   PM User | #3
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 790
Thanks: 208
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Hello

This seems to be working fine and is also efficient.

Many thanks for your help.
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:32 AM.


Advertisement
Log in to turn off these ads.