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

  • SEARCH
  • Home
  • 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 7516289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:04:56+00:00 2026-05-30T01:04:56+00:00

I’ve been struggling with a custom jQuery plugin. The entire point of it is

  • 0

I’ve been struggling with a custom jQuery plugin. The entire point of it is the following: you click the trigger, a toolbox comes up. In that specific toolbox you have one input field in which you paste and submit a Youtube or Vimeo URL. Based on that URL I’m changing the video that’s currently on the page.

The issue I’m having is that when I click the trigger, I get not one, three toolboxes ( if I have 3 videos on the page and I click the first one ), two toolboxes ( if I have 3 videos on the page and I click the second one ), one if I click the last one ( same condition ) – I’m pretty sure you know where this is going.

Here’s the code:

(function($) {

$.fn.videowidget = function() {
return this.each(function(){    
    // declare variables
    var parent = $(this);
    var thisPos = $(this).offset();

    var widgetHtml = jQuery('<div class="tool-video"><ul><li><a href="#tool-video1">Video</a></li></ul>' +
                    '<div id="tool-video1">' +
                            '<form id="tool-video-form" action="#" method="post">' +
                            '<label for="tool-video-url">Please enter the URL of your video ( only Youtube or Vimeo accepted )</label>' +
                            '<input type="text" id="tool-video-url" name="tool-video-url" value="" class="marginFive">' +
                            '<a href="#submitVideo" class="videowidget-submit btn btn-success">Submit</a>' +
                            '<div class="tool-video-error"></div>' +
                        '</form>' +
                    '</div>' +
                    '<a href="#close" class="closeImageBox">Close</a>' + 
                    '<a href="#drag" class="dragHandler" title="Drag me !!!">Draggable</a>' +
                    '</div>');

    // check if the containing div has the class 'w-video'
    if($(this).hasClass('w-video')) {
        $(this).append('<a href="#video" class="videoPlaceholder">Video placeholder</a>');
        $('.videoPlaceholder').bind('click', function() {

        // insert the video widget and apply the required settings ( positioning, drag, tabs )
        widgetHtml.appendTo('body').css(thisPos).fadeIn().draggable({handle: 'a.dragHandler', cursor: 'move'}).tabs();
        $('.videowidget-submit').click(function(){
            // value of the submitted url
            var url = $(this).prev('input').val();
            // regex to match provider
            var provider = url.match(/(?:http:\/\/)?(:?www.)?(\w*)/)[2], id;
            if(provider == "youtube") {
                id = url.match(/(?:http:\/\/)?(?:www.)?(\w*).com\/.*v=(\w*)/)[2];
                // remove the curent iframe and replace it with the one bellow using the ID of the submitted URL
                var youtubeTemplate = '<iframe width="460" height="259" src="http://www.youtube.com/embed/'+ id +'?wmode=opaque" frameborder="0" allowfullscreen></iframe>';
                parent.find('iframe').remove();
                parent.append(youtubeTemplate);
                $('.tool-video-error, .tool-video').fadeOut();
                return false;
            } else if (provider == "vimeo") {
                id = url.match(/(?:http:\/\/)?(?:www.)?(\w*).com\/(\d*)/)[2];
                // remove the curent iframe and replace it with the one bellow using the ID of the submitted URL
                var vimeoTemplate = '<iframe src="http://player.vimeo.com/video/'+ id +'?wmode=opaque" width="460" height="259" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
                parent.find('iframe').remove();
                parent.append(vimeoTemplate);
                $('.tool-video-error, .tool-video').fadeOut();
                return false;
            } else if (provider == "youtu") {
                id = url.match(/(?:http:\/\/)?(?:www.)?(\w*).be\/*(\w*)/)[2];
                // remove the curent iframe and replace it with the one bellow using the ID of the submitted URL
                var youtubeTemplate = '<iframe width="460" height="259" src="http://www.youtube.com/embed/'+ id +'?wmode=opaque" frameborder="0" allowfullscreen></iframe>';
                parent.find('iframe').remove();
                parent.append(youtubeTemplate);
                $('.tool-video-error, .tool-video').fadeOut();
                return false;
            } else {
                // throw error if the submitted URL doesn't match youtube or vimeo
                $('.tool-video-error').html('Error: The URL you submitted doesn\'t appear to be valid ').fadeIn();
            }
                return false;
            });
            // close the toolbox
            $('.closeImageBox').click(function(){
                $(this).parent().fadeOut();
                return false;
            });
        return false;
        });
    } else {
            // do nothing
    }
});
};
})(jQuery);
  • 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-05-30T01:04:58+00:00Added an answer on May 30, 2026 at 1:04 am

    The problem is that you don’t provide a context for your selectors when you select by class to do the bindings.

    $('.videowidget-submit').click(...)
    

    So when you have several elements in the page onto you apply your plugin, it binds to all elements with class “videowidget-submit” instead of just the current instance.

    Add a context to the followin selectors, like this (I may have forgotten some, check you code).

    For the <a> link to open the popup:

    $(this).find('.videoPlaceholder').bind('click', ...)
    

    For the elements inside the popup:

    widgetHtml.find('.videowidget-submit').click(...)
    
    widgetHtml.find('.tool-video-error, .tool-video').fadeOut()
    
    widgetHtml.find('.closeImageBox').click(...)
    

    DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
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.