C.Roshan C.Roshan - 1 year ago 67
HTML Question

I need to get id from HTML using jQuery

This is my code:

function preloadImages(images, callback) {
var count = images.length;
var loaded = 0;
$(images).each(function() {
$('<img>').attr('src', this).load(function() {
if (loaded === count) {

"../images/Wedding_p/large/5.jpg"], function() {
// In here I need to get id of '.image1' image.

this is html:

<img src = '1.jpg' data-id='1' class = 'image1'>
<img src = '2.jpg' data-id='2' class = 'image1'>
<img src = '3.jpg' data-id='3' class = 'image1'>

When I click on the image I need the data-id of clicked image; how can I get it using my jQuery code.

Answer Source

Try this:

$(document).on('click', '.image1', function() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download