Selectors that can be applied to multiple IDs with identical functions

I have a script for popups that I want to be able to use multiple instances of. For example, I would like to have 5 popups on one page without having to change the JavaScript code.

While everything in the script works fine, I am struggling to make it work under multiple instances.

I would like the script to target elements with ids starting with 'popup_', so that I can have popups named popup_one, popup_two, and so on, without needing to modify or add to the JavaScript code.

Any assistance would be greatly appreciated.

Answer №1

To target elements using class selectors, start by returning each element in your plugin to match multiple elements.

    $.fn.expose = function(options) {
       return this.each(function() {
         var $modal = $(this),
         $id = $(this).attr("id");
         $trigger = $("a[href=#" + $id + "]");
         //...rest of the stuff
       }     
    }

When defining divs, assign a class name to them

    <div id="Popup_normal" class="Modal expose_modal" >
      //rest of stuff
    </div>

Finally, call the function using a class selector

$(".expose_modal").expose();

VIEW DEMO

Answer №2

To implement the desired functionality, simply utilize the code below:

$("#Popup_normal").expose();

You only need to provide an id for this function and there is no requirement to modify the javascript .expose() function.

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

The focal point of a Three JS rotation

My goal is to rotate the geometry around a pivot point and set that as the new definition of the geometry. I want the current rotationZ to become the new rotationZ 0 without having to keep editing the rotationZ. This way, when I create a new rotation task ...

Discover the power of AngularJS's ng-route feature for creating a dynamic and seamless one-page HTML template experience

I'm attempting to create a dynamic header using ng-repeat. Here's the initial code: <ul class="right"> <li><a href="#carousel">Home</a></li> <li><a href="#about-us">About Us</a></li> &l ...

Set up AngularJS application to save API URLs

In configuring my API endpoint and URL's, I utilize a custom app.config.js file with environment variables: angular.module('api-config', []).constant('ENV', { name: 'My Project Name', apiEndPoint: 'http://SO ...

Unable to fetch the identification number from the database

I'm encountering an issue with retrieving the ID from my database: https://i.sstatic.net/oSAi8.jpg Here is a snapshot of my database: https://i.sstatic.net/j5PpZ.jpg Below is my event controller class: <?php namespace App\Http\Contro ...

Delaying the activation of the code until the image upload is complete

I'm having trouble synchronizing code to upload an image using a vue composable, wait for the upload to finish, and then store the Firebase storage URL into a database. Despite getting the URL, the success code fires before the upload is complete. My ...

Mastering the correct application of flex properties within nested flex containers

I'm struggling with utilizing flexbox properly and would like some clarification on how nesting parent and child elements functions. I understand that the child elements inherit the parent's flex properties, but I'm unsure if this inheritan ...

Navigating through Objects in Angular 9

I am facing a challenge in Angular 9/Typescript while trying to iterate through the object response from my JSON data. Despite searching for solutions, I haven't found any that work for me. In my JSON, there is a section called "details" which contain ...

Switch back and forth between different information

My Bootstrap version 4.1.1 page needs a tweak. I want to make the background color of the active toggle white. .nav-tabs { margin-top: 3%; border: none; background: #0062cc; border-radius: 1.5rem; width: 28%; float: right; } .nav-tabs .nav- ...

Is there a way to rearrange the tabs so that the first tab is at

Is there a way for me to align with the others without being stuck below #first_tab in the code? I'm having trouble figuring it out on this website: Your assistance would be greatly appreciated! Thank you in advance! CSS Code: #first_tab { ...

Split a lengthy inventory into separate pages using EJS in combination with jQuery and AJAX

Currently, I am developing a dashboard using Node.js and express. At times, the lists I generate are excessively long, and I need to split them into pages with 20 items each. Users should be able to navigate between pages using buttons. However, I am unsur ...

What could be causing my div element to not inherit its parent div's width?

My query revolves around a header div that I'm struggling to align with the same width as its parent div, while keeping the content centered. Currently, the div is only as wide as its content, causing it to be off-center. https://i.stack.imgur.com/E1 ...

Tips for creating a div that gracefully fades out its background image when hovered over, while keeping the internal content unaffected

I am looking to create a hover effect on a div element that has a background-color, background-image, and text. What I want is for the background-image to slowly disappear when the div is hovered over, while keeping the text and background color visible. I ...

Guide on using double quotation marks for keys and values in a JavaScript array

I have an array that contains buffer data shown below [{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> }] I need to add double quotes to bot ...

Any suggestions on how to address vulnerabilities in npm when upgrading the main dependency version is not an option?

I recently ran the npm audit --production command and found a high-risk vulnerability related to the snowflake-sdk dependency. After checking the snowflake github page, I noticed that the package.json file includes "requestretry": "^6.0.0&qu ...

Trigger a JavaScript event when attempting to input the maximum number of characters

I am currently facing a challenge with implementing a tooltip on a text area that has a maximum character limit of 500. The tooltip should only appear when the user reaches the maximum character count and attempts to input more characters. My previous appr ...

Different style sheets & Susy: Tailored designs for varying orientations

I have the task of designing two layouts - one for Landscape and one for Portrait. The Landscape layout needs wider columns. I am utilizing the latest version of Susy to accomplish this. Below is the code snippet I have written: /* iPads (landscape) ---- ...

Hovering over graphics for automatic scrolling

I have successfully implemented a hover effect that scrolls down an image inside a div. However, I am facing an issue with making the scroll longer for images of varying lengths by using calc inside the transition property. Below is the code snippet that ...

Strategies for efficiently updating specific objects within a state array by utilizing keys retrieved from the DOM

I am trying to figure out how to use the spread operator to update state arrays with objects that have specific ids. Currently, I have an array called "todos" containing objects like this: todos: [ { id: "1", description: "Run", co ...

QML: Inside and outside styling attributes

Within my QML Text (RichText) elements, I have incorporated multiple CSS-styled html elements using the inline 'style' attribute within each tag (e.g. <code style=\"background-color:black; text-indent: 10px\"></code&g ...

Typescript's Nested Type Assignments

Simply put, I'm making an API call and receiving the following data: { getUserInfo: { country: 'DE', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c48594f487c59445d514c5059125f5351">[e ...