/    Sign up×
Community /Pin to ProfileBookmark

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>
[/CODE]

Any neat solution for this?

Thanks in advance.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@beau_kangNov 08.2011 — I haven't tested it, but from what I'm understanding on what you want to do, you might want to try this:
[CODE]
<script type="text/javascript">

$(document).ready(function(){

$('*, a:not(.mylink)').click(function(){
alert(123);
});


});

</script>
[/CODE]


That should make your alert happen on everything except for anything that is a link with the class "mylink". The * is an everything selector.
Copy linkTweet thisAlerts:
@aj_nscNov 08.2011 — The below code would be more efficient as the selector performance of '*, a:not(.mylink)' is undoubtedly horrid.

<i>
</i>(function($) {
$(document).ready(function() {
$(document).bind('click',function(e) {
var el = (e.target) ? e.target : e.srcElement:

<i> </i> if(!$(el).hasClass('mylink')) {
<i> </i> alert('do alert');
<i> </i> }

<i> </i> });
<i> </i>});
}(jQuery));
Copy linkTweet thisAlerts:
@phantom007authorNov 09.2011 — Hi Guys

Thanks for your replies.

I will try out these examples.
×

Success!

Help @phantom007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...