[jQuery] My first plugin, criticisms please
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