Attempting to connect JQuery to a different page through a data attribute connection

I am currently working on a slider that connects slides from an external page through data attributes.

Here is the setup:

Main Page

<div id="sitewrapper">
    <section class="sliderwrapper">
        <div id="slider" data-source="slides-page.html" data-speed="500" data-easing="swing">
            <ul>

            </ul>
        </div>
        <div id="loader"><span id="load"></span></div>
        <a class="button" id="back"></a>
        <a class="button" id="next"></a>
    </section>
<!--end sitewrapper-->
</div>

<script>
    $(function() { //run when the DOM is ready
   $('.internalclick').click(function(e) {
       alert("I clicked Area Tag" + $(this).attr('data-direct'));
      });

    });
</script>

Slides page (slides-page.html)

    <li class="hslide" data-id="1"><img src="content/01.png" usemap="#Map">
      <map name="Map">
        <area shape="rect" coords="331,158,538,297" href="#" class="internalclick" data-direct="I-need-to-direct">
        <area shape="rect" coords="546,154,745,305" href="#">
        <area shape="rect" coords="331,311,543,445" href="#">
        <area shape="rect" coords="550,309,733,443" href="#">
      </map>
    </li>
    <li class="hslide" data-id="2"><img src="content/02.png"></li>
    <li class="hslide" data-id="3"><img src="content/03.png"></li>
    <li class="hslide" data-id="4"><img src="content/04.png"></li>

Issue

While everything seems to be working fine, I encountered a problem when attempting to add a Click function to specific list elements in the slides page. The issue arises with

href="#" class="internalclick" data-direct="I-need-to-direct"
. I am having difficulty accessing it.

If anyone has any suggestions or ideas on how to create a click function that targets the area tag in the slides page, please let me know.

Thank you.

Answer №1

The issue arises from the fact that the script for internalclick is being loaded after the event handlers are added. To resolve this, you should implement event delegation.

$("#sitewrapper").on("click", ".internalclick", function(e) { 
    var direct = $(this).data("direct");
    console.log(direct);
});

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

I am looking to update the background color of the material UI helper text

In the image below, you can see that my background color is gray and my text field color is white. When an error is shown, the text field's white color extends and the password error message doesn't look good. I want the text field to remain whit ...

PHP: Using conditional statements to adjust datetime formatting for CSS styling

My title may not make sense, as I am new to PHP. I'm trying to create a custom member tag for my forum that shows "X Years of Experience" with different colors based on the number of years. For example, red for under 6 months, blue for year one, yell ...

Troubleshooting: Django project not functioning properly post transfer from Windows to Mac

Using Django 3.1.7 on a MacBook Pro now, I recently moved the Django project from a Windows 10 machine. Despite setting up the database and superuser again, running pipenv sync didn't result in serving any of my URLs or Admin page CSS styles. The serv ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

The function auth.createUserWithEmailAndPassword is not recognized in the context of React

base.jsx: import { initializeApp } from "firebase/app"; import { getAuth } from "firebase/auth"; const firebaseConfig = { config }; export const app = initializeApp(firebaseConfig); export const auth = getAuth(); Register.jsx ...

There was an issue encountered while parsing the JSON data - "SyntaxError: Unexpected token . was caught."

Encountering an error in Chrome while parsing JSON data. The JSON sample can be found at this link. It is valid JSON and the server is sending the correct Content-Type value (application/json). Uncaught SyntaxError: Unexpected token . Firefox shows a sli ...

Utilizing the Vuetify Dialog Component in a repetitive manner to confirm the deletion of an event

In a project I'm working on, there's a 'datatable.vue' file that loops through data and displays it in a table. Within this loop, I want to implement a reusable dialog component from Vuetify (v-dialog) that will load upon interaction wi ...

How can I easily add both horizontal and vertical arrows to components in React?

Creating a horizontal line is as simple as using the following code: <hr style={{ width: "80%", border: "1px solid black" }} /> You can customize the width, length, and other properties as needed. If you want to display an arrow ...

Crafting the Perfect HTML Table Layout

I'm working on a rather intricate table that needs to be created. I've managed to create a simple one, but I could use some guidance on how to proceed. Desired Table Structure Current Table Structure <table class="ccTbl" border="0" cellpadd ...

The submit function in jQuery is failing to send the form data when used with input types of submit

I need help submitting a form with two different submit buttons. Below is the HTML code: <form enctype="multipart/form-data" id ="type-1" accept-charset="UTF-8" action="process_upload.php" method="post" onsubmit="return Genome_upload_validation()" > ...

Problem with Ionic App crashing

Currently, I am developing an Ionic app that relies on local storage for offline data storage. The app consists of approximately 30 different templates and can accommodate any number of users. Local storage is primarily used to store three key pieces of i ...

What is the process for transferring data between components in React?

Currently, I have set up a system to display an employee table from the database. Each record in the table includes an "edit" link which, when clicked, should trigger the display of a form below the table containing the corresponding records for editing. T ...

Implementing TestCafe with a Rails backend and utilizing fixtures data for testing

Currently, I am involved in a Rails project that utilizes RSpec for testing. We rely on various Rails fixture data to test our UI under different conditions. Recently, I came across TestCafe for handling functional UI testing, and I find it quite intrigui ...

In a production environment, an ENOENT error in Next.js was triggered by fs.readdirSync

Utilizing the Next.js App Router, I attempted to retrieve all my markdown posts stored in files by scanning a directory using fs.readdirSync(). While everything worked flawlessly locally, upon deploying on Vercel, an unexpected issue arose. The code was e ...

Reordering the stacking order of Grid Items in material-ui

I'm facing an issue with the stacking order of grid items when the browser shrinks. On a regular desktop screen (lg): --------------------------------------------- | col 1 | col 2 | col 3 | --------------------------------------- ...

Function wrapper intended for axios

How can I create a wrapper function for axios? I am looking to create a function that can return axios, allowing for easy substitution with another fetch API in the future. This is what I have attempted so far: import axios from 'axios' expor ...

What is the essential Angular 2 script that must be included for a simple Angular 2 application to function properly?

I'm currently working through the latest Tuts+ tutorial on Angular 2 In the tutorial, the author references adding this script: <script src="node_modules/angular2/bundles/angular2.sfx.dev.js"></script> However, in the most recent beta re ...

Enhance user experience with Bootstrap by automatically adding a frame around a card upon clicking

Hello everyone! I am a beginner in the Angular/Web Development world and I am currently working on improving my HTML/CSS skills. While using Bootstrap in my Angular project, I ran into a challenge that I couldn't figure out on my own. I have implement ...

Should I utilize a single form or one for each table row in asp.net mvc?

While working on a project, I caught myself in a coding dilemma: <table> <tr> <th>Code</th> <th>Enabled</th> <th>Percentage</th> <th>Amount</th> <th>Start&l ...

Adding a div to the preceding div based on matching IDs in AngularJS

Here is a representation of my layout in HTML: <div ng-repeat="message in messages"> <div ng-class="{'messages messages--sent': userId == message.id, 'messages messages--received': userId != me ...