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 7161423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:33:52+00:00 2026-05-28T13:33:52+00:00

I have been developing a jquery widget that extends ui.mouse. The widget needs to

  • 0

I have been developing a jquery widget that extends ui.mouse.

The widget needs to react to mouse events by creating elements and appending them to this.element.

In the case that the widget is applied to an HTML element such as an IMG tag that cannot contain child elements, the widget creates a surrogate wrapper element and wraps the this.element.

If I replace this.element with the wrapper element events that the widget triggers are never handled because .bind() calls on the widget instance apply the handler to the original element, not the wrapper.

If I don’t replace the this.element with the wrapper element then the _mouse* events defined by ui.mouse are not called correctly because they are applied to this.element not the wrapper.

Is there an elegant way of somehow returning the wrapper element so that subsequent bind() calls are applied to it or making the ui.mouse prototype apply to an element other than this.element?

Any other elegant solution or advice would be very welcome.

These are the two scenarios I have tried.

Without replacing this.element

$.widget("ui.example", ui.mouse, {

    _containerElement: null,

    _create: function ()
    {
        if (this.element[0].nodeName.match(/canvas|textarea|input|select|button|img/i))
        {
            this._applyContainer();
        }else{
            this._containerElement = this.element;
        }

        // Applies mouse interaction handlers to this.element
        this._mouseInit();

    },

    _applyContainer: function () 
    {
        this.element.wrap("<span>");
        this._containerElement = this.element.parent();
        this._containerElement.css({position:'relative'});
    },


    _mouseStart: function (event) {

        // Not always called because it handles mouse interaction with
        // the IMG element rather than the wrapper element

        this._trigger("mouseevent");
    }
})

$("IMG").example().bind("examplemouseevent", function(){
    //This fires but only when the original IMG element is clicked, not the wrapper
})

Replacing this.element

$.widget("ui.example", ui.mouse, {

    _create: function ()
    {
        if (this.element[0].nodeName.match(/canvas|textarea|input|select|button|img/i))
        {
            this._applyContainer();
        }

        // Applies mouse interaction handlers to this.element
        this._mouseInit();

    },

    _applyContainer: function () 
    {
        this.element.wrap("<span>");
        this.element = this.element.parent();
        this.element.css({position:'relative'});
    },

    _mouseStart: function (event) {

        // This event is never handled because it is not raised on the original element. 
        this._trigger("mouseevent");
    }
})

$("IMG").example().bind("examplemouseevent", function(){
    //This never fires because the bind is to the IMG element, not the wrapper
})

I’m pretty sure the issue I’m having is due to the shortcomings of the way I understand jquery ui.mouse to work, or the restrictions of the jquery widget framework.

Replacing this.element with an element that wraps the origional this.element works but events are NOT passed to handlers that are bound to the widget later.

Not replacing this.element and storing a seperate reference to the wrapper causes the behavior of the jquery base ui.mouse to attach events to the wrong element.

The code is working as it is designed, my problem finding the best way to work with the restrictions of the design.

I can see a number of ways of addressing this. For example; duck-punching the _trigger method to trigger the event on the correct element or swapping the this.element value backwards and forwards while ui.mouse attaches the required handlers.
These and any other methods I have considered seem pretty messy.

I’ve looked at the native jquery.resizable widget code that also creates a wrapper but from what I can tell, it would also suffer from the same issue if it attempted to trigger events.

This is the first time I’ve worked with JQuery widgets so I’d like to confirm from a JQuery guru that im not missing something here?

  • 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-28T13:33:53+00:00Added an answer on May 28, 2026 at 1:33 pm

    I’m not entirely satisfied with it but the solution I’ve gone with is to swap out this.element call _mouseInit and swap it back again.

    _containerElement: null,
    
    _applyContainer: function () 
    {
        var origionalElement = this.element;
        this.element.wrap("<div>");
        this._containerElement = this.element.parent();
        this.element = this._containerElement;
        this._mouseInit();
        this.element = origionalElement;
        this._containerElement.css({position:'relative'});
    },
    

    I’s not beautiful, but it works. I might have a go at overriding _mouseInit at some point in the future but this suits my purposes for now.

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

Sidebar

Related Questions

I have been developing in asp.net since its existence (also classic asp before that)
I am developing a project that lately have been taking off with increased popularity.
I have been developing Web Applications using WAMP stack. I use javascript jQuery extensively
I'm developing a website(just for fun) and have been using mostly js/jquery to build
I have been developing a parser that takes JavaScript as input and creates a
I have been developing my first mobile site with jQuery Mobile and you can
I have been developing and testing with inline jQuery. I am using CodeIgniter and
I have been using jquery mobile for developing a restaurant e-menu for ipad and
People have been developing own solutions to the following problems: Consistent messaging frameworks for
I have been developing websites for a couple of years now and I almost

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.