Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:57:26+00:00 2026-06-01T02:57:26+00:00

I’m a bit stuck on this – I am using a WordPress shortcode with

  • 0

I’m a bit stuck on this – I am using a WordPress shortcode with a modal overlay plugin to insert multiple overlays into the content editor. However, I’m trying not to repeat my jQuery code multiple times for each ID.

As you can see in the shortcode I can set the ID of the overlay in WordPress’s editor like so:

Shortcode

[osu_overlay linktext="Text for link" oid="1"] ... content for overlay ... [/osu_overlay]

This creates the following HTML markup:

HTML

<a id="overlaylink-1" class="overlaybox" rel="overlaybox-1" href="javascript:void(0);">Text for link</a>
<div id="overlaybox-1" class="overlaybox" style="display:none;"> ... content for overlay ...</div>

This is the jQuery I don’t want to have to repeat for each ID i.e. for overlaylink-1, overlaylink-2 etc.

jQuery

// Check if .overlaylink exists first
if($("#overlaylink-1").length == 0) {
    // #overlay-link-1 doesn't exist
} else {
    // Start overlay
    $('#overlaylink-1').click(function(){
        function dialogFadeIn() {
            dialogFadeIn.data.fadeIn('slow');
        }
        $("#overlaybox-1").modal({
            persist: true,
            onOpen: function(dialog) {
                dialog.overlay.fadeIn('medium', function () {
                    dialog.data.hide();
                    dialog.container.show()
                    dialog.data.fadeIn('medium');
                });
            }
        });
    });
}

Is there a way of writing the jQuery code above so that it accounts for all IDs i.e. ‘overlaylink-x’ and overlaybox-x’ where x is any number?

Thanks for any help

Osu

–*– EDIT –*–

Here’s the final jQuery code – I’ve updated the HTML and Shortcode as well to include rel=”” which is needed for this jQuery plugin code to work. Thanks to @DingoEatingFuzz for this solution (below).

New jQuery

// Check if .overlaylink exists first
if($(".overlaylink").length == 0) {
    // nada bc doesn't exist
} else {

    // Get Box to overlay from link rel
    var link = $('overlay-link-1'),
        box = $('#' + link.attr('rel'));

    // Plugin for showing overlay
    (function($) {
        $.fn.osuModal = function(options) { 
            // $.fn is the jQuery plugin object
            // make sure to loop in case the selector specified returns more than one object
            // make sure to return to support function chaining
            return this.each(function() {
                var $this = $(this);
                $this.click(function() {
                    function dialogFadeIn() {
                        dialogFadeIn.data.fadeIn('slow');
                    }
                    $('#' + $this.attr('rel')).modal({
                        persist: true,
                        onOpen: function(dialog) {
                            dialog.overlay.fadeIn('medium', function() {
                                dialog.data.hide();
                                dialog.container.show();
                                dialog.data.fadeIn('medium');
                            });
                        }
                    });
                }); // End '$this.click(function()'
            }); // End 'return this.each(function()'
        }
    })(jQuery);

    // Run the plugin
    $('.overlaylink').osuModal();

}

WP Shortcode PHP

This is in case anyone wants to do the same in WordPress (you place this in your functions.php file):

// Shortcode for overlays
function osu_overlay($atts, $content = null) {
    extract(shortcode_atts(array('linktext' => '#', 'oid' => '#'), $atts));
    return '<a class="overlaylink" id="overlaylink-' . $oid . '" rel="overlaybox-' . $oid . '" href="javascript:void(0);">' . $linktext . '</a><div class="overlaybox" id="overlaybox-' . $oid . '" style="display:none;">' . do_shortcode($content) . '</div>';
}
add_shortcode('osu_overlay', 'osu_overlay');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:57:27+00:00Added an answer on June 1, 2026 at 2:57 am

    Although there are ways to do what you are asking, I think you are creating a problem for yourself that can be avoided with different markup.

    I would reference the div in the a tag like this:

    <a href='#' id='overlay-link-1' rel='overlaybox-1'>Text For Link</a>

    This way, the selector for the box can be derived from the link.

    var link = $('overlay-link-1'),
        box = $('#' + link.attr('rel'))
    ;
    

    Now id of the div and the id and rel of the anchor tag aren’t important, as long as they match. The last step is to abstract the modal feature into a plugin for a simple interface.

    (function($) {
        $.fn.osuModal = function(options) { // $.fn is the jQuery plugin object
            // make sure to loop in case the selector specified returns more than one object
            // make sure to return to support function chaining
            return this.each(function() {
                var $this = $(this);
                $this.click(function() {
                    function dialogFadeIn() {
                        dialogFadeIn.data.fadeIn('slow');
                    }
                    $('#' + $this.attr('rel')).modal({
                        persist: true,
                        onOpen: function(dialog) {
                            dialog.overlay.fadeIn('medium', function() {
                                dialog.data.hide();
                                dialog.container.show();
                                dialog.data.fadeIn('medium');
                            });
                        }
                    });
                }
            }
    })(jQuery);
    

    Lastly, call the modals however you feel like.

    $('.generic-class-for-all-modals').osuModal();
    $('#specific-modal').osuModal();
    $('#generated-modal-1').osuModal();
    $('#generated-modal-1 #generated-modal-2').osuModal();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.