My div has no id . can i call a jquery function on click of that div
div identified by class used in div.
<div class="divclass">
how can i call
$('.divclass').click(
???
My div has no id . can i call a jquery function on click of that div
div identified by class used in div.
<div class="divclass">
how can i call
$('.divclass').click(
???
Here is the correct syntax:
$('.divclass').click(function(){
alert('I got clicked !');
});
To trigger a click on the div programatically, you can do:
$('.divclass').click();
See:
To assign a click handler, you can do this:
$('div.divclass').click(function() {
alert('i was clicked');
});
...or this, which is the same thing:
$('div.divclass').bind('click', function() {
alert('i was clicked');
});
Here's an example: http://jsfiddle.net/MAKHh/
EDIT:
As noted by @mcgrailm, there's another means of firing code on the click of an element called .live(). As well as a related one called .delegate().
With live(), the handler will fire regardless of when the element is added to the page.
$('div.divclass').live('click', function() {
alert('i was clicked');
});
Or with .delegate() you call it agains a container, and elements added to that container at any time will fire the handler.
$('.someContainer').delegate('div.divclass', 'click', function() {
alert('i was clicked');
});
Or if you wanted to .trigger() a click event, do this:
$('div.divclass').click();
Or this:
$('div.divclass').trigger('click');
You can also call the handler without triggering the event.
$('div.divclass').triggerHandler('click');