JQuery - Programmatically Trigger Event

I need to programmatically fire the click event, which is handled by jQuery. Here's the current code:

var $thumbs = $('#PhotoGalleryThumbs .tile');
var $zoom = $('#PhotoGallery #PhotoGalleryZoom img');
var $description = $('#PhotoGallery #PhotoGalleryDescription');

$thumbs.click(function(event) {
    event.preventDefault();
    var $thumb = $(this);
    $thumb.addClass('selected')
        .siblings().removeClass('selected');
    $zoom.attr('src', $thumb.children('a').attr('href'));
    $description.html($thumb.find('img').attr('alt'));
});

I have a mental block defining how to create a function from the event processing code, and then arbitrarily call it for an element in the object $thumbs.

+3
source share
2 answers
$thumbs.click();

This will trigger a click event. Is this what you are looking for?

+5
source

Like the previous sentence

$($thumbs[7]).click();

you can use

$thumbs.eq(7).click();

jQuery DOM , .eq(n) jQuery, .

http://api.jquery.com/eq/

+2

Source: https://habr.com/ru/post/1742491/


All Articles