Exitos
Exitos

Reputation: 29720

Is there a way to compress this javascript code down more?

I have written the following code which works, but im wondering can I make it any smaller.

$(document).ready(function ()

            $('.datepickerTarget').datepicker({
                onSelect: function (dateText, inst) {

                    $('#' + this.id).removeClass('watermark');

                }
            });

            $('.datepickerTarget').datepicker("option", "dateFormat", "dd/mm/yy");

        });

I guess im trying to understand what the line:

$('.datepickerTarget').datepicker("option", "dateFormat", "dd/mm/yy");

Is doing. Is this a constuctor? Where do I find what i can put in this line? Is it just a method? Not sure about the syntax.

Hope someone can help me understand so I can shorten this code make it a bit more slick...

Upvotes: 0

Views: 111

Answers (7)

dereli
dereli

Reputation: 1864

This should help.

$(document).ready(function () {

    $('.datepickerTarget').datepicker({ onSelect: function () { $(this).removeClass('watermark'); }, dateFormat: "dd/mm/yy" });

});

Upvotes: 1

Patricia
Patricia

Reputation: 7802

$('.datepickerTarget').datepicker({
     onSelect: function (dateText, inst) {
                   $('#' + this.id).removeClass('watermark');
               },
     dateFormat : "dd/mm/yy"
});

it's not really actually less lines, but it's tidier to move the dateFormat into the datepicker initiation.

you really should have a look at the documentation!

Upvotes: 0

Grant Thomas
Grant Thomas

Reputation: 45083

It doesn't look very compressed at all already - usually, compressing Javascript boils down to obfuscating it, in a sense: removing all unnecessary characters, including newlines and even renaming methods to something much less (or not at all) descriptive. You might mean compress in another sense, such as removing non-required code to minimise not only the amount, but also ridding code of superfluous work - such would be removing redundancy.

With that out of the way, and moving onto your second question, yes, that is something of a constructor, or "initializer"; used to instantiate the plugin and supply required arguments as parameters. You should be able to find out which parameters are supported in two ways:

  • Read the documentation for the plugin
  • Look at the source file and read the code

Upvotes: 0

Doug Moscrop
Doug Moscrop

Reputation: 4544

Making code more slick is a step towards making it unmaintainable.

If you're having a hard time figuring out how to make it smaller, think of the hard time you will have remembering what the hell it does when you look at it in 6 months.

Upvotes: 4

Gabe
Gabe

Reputation: 50493

Yes, you can set the dateFormat property along with the onSelect in on single call here:

$('.datepickerTarget').datepicker({
            onSelect: function (dateText, inst) {
                $('#' + this.id).removeClass('watermark');
            },
            dateFormat: "dd/mm/yy"
});

Upvotes: 1

Keith.Abramo
Keith.Abramo

Reputation: 6955

$(document).ready(function ()
        $('.datepickerTarget').datepicker({
            onSelect: function (dateText, inst) {

                $('#' + this.id).removeClass('watermark');

            },
            dateFormat: "dd/mm/yy"
        });
    });

You can add that option right in the initialization options of datepicker

Upvotes: 1

arb
arb

Reputation: 7863

$(document).ready(function ()
    $('.datepickerTarget').datepicker({
        dateFormat:"dd/mm/yy",
        onSelect: function (dateText, inst) {
            $('#' + this.id).removeClass('watermark');
        }
    });
});

You can set the date format when the object is constructed by passing it as a parameter to the "constructor" of the date time object.

If you are worried about size, look into JavaScript compression. I use the YUI compression algorithm.

Upvotes: 2

Related Questions