tags:

views:

1881

answers:

1

I am in the process of creating my first jQuery plug-in that will format numbers automatically to various international formats. There are a couple functions within the plug-in that strips the strings and re-formats the string that needs to be called from another jQuery script.

Based on the plug-in structure below (let me know if you need the entire code) can I call and send the parameter(s) to the stripFormat(ii) and targetFormat(ii, iv) functions?

Or do I need to change my plug-in structure and if so how?

    (function($){
        var p = $.extend({
            aNum: '0123456789',
            aNeg: '-',
            aSep: ',',
            aDec: '.',
            aInput: '',
            cPos: 0
        });

        $.fn.extend({
             AutoFormat: function() {
                return this.each(function() {
                    $(this).keypress(function (e){
                        code here;
                    });

                    $(this).keyup(function (e){
                        code here;
                    });

                    // Would like to call this function from another jQuery script - see below.
                    function stripFormat(ii){
                        code here;
                    }

                    // Would like to call this function from another jQuery script - see below.
                    function targetFormat(ii, iv){
                        code here;
                    }

                });

            }
        });

    })(jQuery);

Methods trying to call the plug-in functions:

    jQuery(function(){
            $("input").change(function (){ //temp function to demonstrate the stripFormat() function.
                document.getElementById(targetElementid).value = targetFormat(targetElementid, targetValue);
            });
    });

I have tried to use these variations without success:

    document.getElementById(targetElementid).value = $.targetFormat(targetElementid, targetValue);
    document.getElementById(targetElementid).value = $.autoFormat().targetFormat(targetElementid, targetValue);
A: 

Well I would probably do it more like this plugin: http://www.stilldesigning.com/dotstring/jquery.string.1.0.js

$.string(" <p>this is a test</p> ").strip().stripTags().capitalize().str
> 'This is a test'

Or take a look at currency format, takes a value and options object. This seems it woudl work for your situation only with other formatting. http://plugins.jquery.com/project/currencyFormat

$('#some_element').currency() //will format the text of the element
$.currency(123456.78,{options}) //formats number and returns string

Also, confused about using document.getElementById("targetElement").value instead of just $("targetElement").val() or $("targetElement").html()

Ryan Christensen
Ryan,Thanks for your suggestions. Here is a link to my plugin http://www.decorplanit.com/plugin/Your suggestions are welcomed.Bob
Bob Knothe
good job dude, the plugin is now complete.
Ryan Christensen
Bob Knothe