Adrian,

Which elements would you likely work on next after copying values?
The order matters because you're going to return a jQuery element or
element set. I think it's better to be able to do something like
this:

$(ELEMENTS_TO_CHANGE).syncValue(ELEMENT_I_WISH_TO_COPY).show();

That way your focus is on the changing element.  Because you're only
reading ELEMENT_I_WISH_TO_COPY I bet it's less likely you'll want to
perform operations on it, so no need to return it for chaining.

Just change the function definition to this:

jQuery.fn.syncValue = function(element, formatter)

And change the return to this:

return this.each(function(i) {
     $(this).attr("value", syncFormatter($(element).attr("value")));
});

Just my 2 cents without running the code.  However, maybe you have a
need to return ELEMENT_I_WISH_TO_COPY for whatever reason.  In that
case the current code is appropriate.  However, since it must be a
single element no need to write 'return this.each...'  Just write 'var
element = this; syncElements.each...' and at the end 'return this'.
Hope this helps!

Charles
doublerebel.com


On Oct 29, 2:20 pm, "Jonathan Sharp" <[EMAIL PROTECTED]> wrote:
> It'd be best to post a link to the sample. Most people won't go through the
> time to copy and paste the above code to execute it.
>
> Cheers,
> -js
>
> On 10/29/07, Adrian Lynch <[EMAIL PROTECTED]> wrote:
>
>
>
> > I hope this is ok to do. I've just done my first plugin and I thought
> > I'd ask for anyone's opinions.
>
> > It takes the value of one input box and assigns it to any number of
> > other input boxes after being run though a formatter function.
>
> > In this example the formatter strips anything that isn't an alpha-
> > numeric character or a full stop (period for the yanks!).
>
> > One thing I'm not sure about is the way I apply the plugin. I'm doing
> > it:
>
> > ELEMENT_I_WISH_TO_COPY.syncValue(ELEMENTS_TO_CHANGE)
>
> > whereas it feels like it should be the other way around. Thoughts
> > anyone?
>
> > Here it is with test code:
>
> > <script type="text/javascript" src="PATH_TO_YOUR_JQUERY"></script>
> > <script type="text/javascript">
>
> >        jQuery.fn.syncValue = function(syncElements, formatter) {
>
> >                var syncFormatter = formatter;
>
> >                if (syncFormatter == undefined) {
> >                        syncFormatter = function(inputString) {
> >                                return inputString;
> >                        };
> >                }
>
> >                return this.each(function(){
>
> >                        var element = this;
>
> >                        syncElements.each(function(i) {
> >                                $(this).attr("value",
> > syncFormatter($(element).attr("value")));
> >                        });
>
> >                });
>
> >        };
>
> >        $(function(){
> >                $(".oringinal").syncValue($(".syncValue"), formatter);
> >        });
>
> >        function formatter(inputString) {
> >                return inputString.toLowerCase().replace(/[^a-z0-9\.]/gi,
> > "-");
> >        }
>
> > </script>
>
> > <input type="text" class="oringinal" value="Value 1" />
> > <input type="text" class="syncValue" value="" />
> > <input type="text" class="syncValue" value="" />
>
> > Thanks all.
>
> > Adrian

Reply via email to