View Full Version : click function problem
The_Alligator
06-30-2010, 07:37 PM
InJQuery , when a html element is clicked , I want to pass the CLASS value of this HTML element to the click(function(){ //blah blah }
How to write that ?
azoomer
06-30-2010, 09:50 PM
I'm having difficulties understanding the question. Can you say some more about what you want to achieve ?
jscheuer1
07-01-2010, 12:37 AM
In jQuery the click function is like - say the element has an id of 'myDiv':
$('#myDiv').click(function(){alert('You clicked me!');});
You can select it many ways - say you want all a tags:
$('a').click(function(){alert('You clicked me!');});
Now the class name could be important. Inside the click function it would be:
this.className
So you could do:
$('a').click(function(){alert(this.className);});
or even:
$('a').click(function(){if(this.className == 'active'){
doSomething();
} else {
doSomethingElse();
}
});
In fact, since an element can have more than one class name, you can use jQuery's hasClass. But then you have to make 'this' into a jQuery object, like:
$('a').click(function(){if($(this).hasClass('active')){
doSomething();
} else {
doSomethingElse();
}
});
If you need more specific help, please explain the specifics, and:
Please post a link to a page on your site that contains the problematic code so we can check it out.
The_Alligator
07-01-2010, 02:24 AM
In jQuery the click function is like - say the element has an id of 'myDiv':
$('#myDiv').click(function(){alert('You clicked me!');});
You can select it many ways - say you want all a tags:
$('a').click(function(){alert('You clicked me!');});
Now the class name could be important. Inside the click function it would be:
this.className
So you could do:
$('a').click(function(){alert(this.className);});
or even:
$('a').click(function(){if(this.className == 'active'){
doSomething();
} else {
doSomethingElse();
}
});
In fact, since an element can have more than one class name, you can use jQuery's hasClass. But then you have to make 'this' into a jQuery object, like:
$('a').click(function(){if($(this).hasClass('active')){
doSomething();
} else {
doSomethingElse();
}
});
If you need more specific help, please explain the specifics, and:
Please post a link to a page on your site that contains the problematic code so we can check it out.
Thanks for the post. Thanks for your time.
You are a very nice person . You explained beautifully.
It was very much helpful to me. I have understood the concept now.
Thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.