jQuery plug-in authoring and/or constructor injection
<p>I've written a plug-in that constructs a rich UI control. I'm hoping that there's a way for me to somehow change how jQuery operate when I query for this element.</p>

<p>I'd like jQuery to be oblivious to the internal structure of this control, but it's not really a hard requirement. What I'd like to do, is to change the meaning of <code>$('#RichUIControlID').val()</code> to some other function because <code>#RichUIControlID</code> is the wrapper element for this control (maybe i use a css class to identify that element).</p>

<p>Currently I have to invoke the plug-in function <code>$('#RichUIControlID').richUIControlID().val()</code> that will return a jQuery object with the necessary changes. <code>val()</code> does in this case what I want it to do.</p>

<p>However, if I could maintain the jQuery interface without that additional function call it would let me continue to use a familiar pattern which has tremendous reuse value in my case.</p>

<pre><code>// I want to change val() depending on some property of the selector #RichUIControlID
$('#RichUIControlID').val()
</code></pre>

<p>Can it be done?</p>

<h1>Edit 1:</h1>

<p>One way to do this thanks to the link provided by mamoo, would be to do it like this then:</p>

<pre><code>(function() {
    var val = jQuery.fn.val;
    jQuery.fn.val = function() {
        if (this.is('#RichUIControlID'))
            return this.richUIControlID().val()
        else
            return val.apply(this, arguments);
    }
})();
</code></pre>

<p>However, it feels a bit too intrusive. Every call to <code>val()</code> shouldn't have to check if it's the #RichUIControlID element, I'd rather hook this up during construction if possible.</p>

<h1>Edit 2:</h1>

<p>This is the stuff, browsing the source code it's actually possible to take control of the jQuery constructor in the same manner. The caveat here is that if the first thing you do is not to forward the call to the default constructor, you'll end up with a troublesome call stack.</p>

<pre><code>(function() {
    var init = jQuery.fn.init;
    jQuery.fn.init = function() {
        var q = init.apply(this, arguments); // call this first
        if (q.data('RichUIControlID'))
            return q.richUIControlID();
        else
            return q;
    }
})();
</code></pre>
