The live feature is operational, but unfortunately the on feature is not functioning

I am facing an issue in my code where replacing .live with .on is not working. I have been trying to identify the problem but haven't found a solution yet.

$(document).ready(function () {
    /*   Reading the data from XML file*/
    $.ajax({
        type: "GET",
        url: "photos.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find('item').each(function () {
                var path = $(this).attr('path');
                var width = $(this).attr('width');
                var height = $(this).attr('height');
                var id = $(this).attr('id');
                var alt = $(this).attr('alt');
                var longdesc = $(this).find('longdesc').text();
                var description = $(this).find('desc').text();
                $('#myImageFlow').append('<img src="' + path + '" id=' + id + '  height="' + height + '"  width="' + width + '" longdesc="' + longdesc + '" alt="' + alt + '"/>');
                imgArr[i] = description;
                imgFront[i] = longdesc;
                i = i + 1;
            });

            $(xml).promise().done(function () {
                console.log("image completed loading");
                $('img:lt(4)').removeClass('t2').addClass('t1');
                $('img:gt(3)').removeClass('t1').addClass('t2');
                $('img#id12').removeClass('t1').addClass('t2');
                $('img#id1').removeClass();


                //alert('Front Initial Text is'+imgFront[1]);
                window.setTimeout(function () {

                    $("#front").html(imgFront[1]);
                    $("#front").css("background", "gray");
                }, 1500);
            });
        }
    });
    $.getScript('js/code.photoswipe.jquery-3.0.5.js');
});

/*front div called when front or back is called */
$('#front').on('click', function (event) {
    event.stopPropagation();
    transitIt(this);
    //  alert('Front Clicked glob   e='+globe);
});


$('#myImageFlow img').on("click", function () {
    event.stopPropagation();
    alert('htmlcalled image Clicked Called=' + clickEnabled);
}); 

Answer №1

Your implementation of event delegation may need some adjustments:

$('#myPictureGallery').on("click", 'img', function(event) {
    event.stopPropagation();
    alert('Image Clicked - HTML call, clickEnabled=' + clickActivated);
});

If #myPictureGallery remains in the document structure, it's a suitable choice for event delegation. Additionally, ensure to include the event parameter in your click handler.

Answer №2

give this a shot -

$('#myImageFlow').on("click", "img", function(event) 
{
    event.stopPropagation();
    alert('htmlcalled image Clicked Called='+clickEnabled);
});

alternatively

$('html').on("click", "#myImageFlow img", function(event) 
{
    event.stopPropagation();
    alert('htmlcalled image Clicked Called='+clickEnabled);
});

Answer №3

Here's a helpful tip:

$('body').on('click', '#myImageFlow img', function() {
    // add your code here
});

Check out these references:

  • .on() - jQuery API Documentation

Answer №4

Did anything appear in the console window?

I noticed that you forgot to include the (event) for the .on method.

Keep in mind that the on function relies on event bubbling to work effectively. Ensure that you select a part of your DOM tree that exists when the page loads and place the children elements you want to be clickable inside the brackets.

http://api.jquery.com/on/

In a delegated-events approach, you attach an event handler to just one element, such as tbody, allowing the event to bubble up only one level (from the clicked tr to tbody):

$("#dataTable tbody").on("click", "tr", function(event){
  alert($(this).text());
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for arranging links in a vertical layout on a navigation bar that is compatible with every device

I'm having trouble aligning the links vertically in the center of the navigation bar, with the logo overlapping the content. Check out the HTML code below: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

Footer that sticks to the bottom of the page across various pages

Having trouble printing a 2-page document on Chrome. The first page has fixed content while the second page has dynamic content in an extended table. The problem I'm facing is keeping the footer at the bottom of the second page. The CSS code below wo ...

What is the best approach for Angular directives: utilizing a single object or separate values as attributes?

This question pertains to best practices, but I do believe there is a definitive answer. I have a directive that offers six customizable options. Should I assign each option to a separate attribute on the directive (as shown below): <my-directive my ...

Pressing the ENTER key does not submit the text box data in Rails/Bootstrap, but clicking the SUBMIT button does

I need assistance with my Rails 4 and Bootstrap 3 project. I have implemented an email address field on my page (localhost:3000) where users can enter their email and click the SUBMIT button to send it to the MongoDB database. However, pressing the ENTER k ...

AngularJS filter that retrieves only values that have an exact match

In my AngularJS application, I have a checkbox that acts as a filter: Here is the data being used: app.controller('listdata', function($scope, $http) { $scope.users = [{ "name": "pravin", "queue": [{ "number": "456", "st ...

Transmit the data through AJAX to a node express server

I have been trying to use AJAX to send data to a node express server, but I am facing difficulties in catching the data using express. I want to know how to properly catch the data using express. Here is the code in my NODE file, but nothing seems to be ...

Issue with IntersectionObserver not detecting intersection when the root element is specified

I am encountering an issue with my IntersectionObserver that is observing an img. It works perfectly when the root is set to null (viewport). However, as soon as I change the root element to another img, the observer fails to detect the intersection betwee ...

Tips for placing a background image on an extended <a> hyperlink in Internet Explorer 6

There seems to be an issue with the background image of a link on my exam project. The requirement is for it to work seamlessly on all browsers, including IE 6. However, the problem arises when the link spans multiple lines in IE 6, causing the background ...

JavaScript Reference for the HTML DOM Style's height property and revert back to the initial style height

Hey there! I have a simple fix for those who know JavaScript. I have some code that changes the height style on the first click, but I'm looking for a way to revert back to the original height style on the second click. Here's the code snippet: ...

Preventing CSS3 animations with unpredictable outcomes

In a recent experiment I conducted, I utilized an animation effect known as "Rotate 3d" in a jQuery library called jq transit. The goal was to have the boxes rotate one by one until they all turned white when the first box was hovered over, and then return ...

Initialize global variables for jQuery objects

Here is an example of some jQuery code that I have been working on. In this code, variables are declared at a global scope, but I have been wondering if it is possible to declare jQuery objects in a similar way to how classes are declared with the cn prefi ...

Analyzing the path of the cursor

Looking to enhance my tracking capabilities by monitoring mouse movements. I have the ability to capture XY coordinates, but understand that they may vary depending on browser size. Are there any other parameters recommended for accurate results? P.S: Be ...

Issues with interpreting PHP within HTML documents

I'm having trouble using PHP on my HTML page. I've added AddType application/x-httpd-php .html to the .htaccess file as recommended, but when I try to access the page in a browser, a dialog box pops up asking if I want to save the file or open it ...

Python code to extract text data from a table using XPath

Recently, I've been using requests and lxml to develop a simple API that can fetch a timetable from a specific website. However, being new to this, I'm struggling to extract any information beyond the hours. Despite experimenting with xpath code ...

Image background within the textarea of Contact Form 7

Struggling to insert a background image inside a textarea on a contact form. The issue arises when the browser window is resized, cutting off the image and failing to fit perfectly within the textarea. I want the textarea to fully display the image and adj ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

Show images dynamically with the help of Flask

Imagine having a camera that can capture real-time images and save them to a folder named "./static". The goal is to showcase each processed image on a local web page using Flask in Python. This means that the system user will be able to view the results ...

Content that is added dynamically seems to vanish once it is posted

Currently, I am working with an asp.net control (.ascx). I am dynamically adding rows to a table using jQuery in the following manner. $('#<%= doorFileNameTable.ClientID %>').after('<tr><td></td><tr/>').a ...

developing a star rating system for a mobile website

Can star ratings be implemented in a mobile website using HTML, CSS, and JS? The HTML code for the stars is as follows: <div class="rating"> <span>☆</span><span>☆</span><span>☆</span><span>☆</sp ...

Guide for adding an OnClick event to a MatTable row:

I am looking to add functionality for clicking on a specific row to view details of that user. For instance, when I click on the row for "user1", I want to be able to see all the information related to "user1". Here is the HTML code snippet: <table ma ...