Search Amazon.com:
Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us $3,400 per month for bandwidth bills alone, and since we don't believe in shoving popup ads to our registered users, we try to make the money back through forum registrations.
  • Post
  • Reply
Kekekela
Oct 28, 2004


Weird Uncle Dave posted:

I'm relatively new to JavaScript generally (I grew up doing everything server-side), and yesterday was my first exposure ever to jQuery. That said, I don't see why this is "better" than my approach.

Unobtrusive javascript is a cleaner approach. The syntax he's using is a jQuery upgrade to the window.onload method used in the wiki article samples.

Kekekela fucked around with this message at Jun 13, 2009 around 04:29

Adbot
ADBOT LOVES YOU

Kekekela
Oct 28, 2004


Metonymy posted:

Lets say I'm using a jQuery AJAX call to pull some JSON and then populate a div with that information using a grim text concatenation ("<div class='gubbawumps'>...") and then a jQuery append(). It seems like the right way to do this is probably with a jQuery.load() and have a page get rendered that contains the HTML I want, rather than trying to generate one in text in javascript. Any thoughts? It's a relatively sizable chunk of content with a lot of markup.

Follow-up: under what circumstances would you use an append()? Stuff with little to no markup?

While I see the allure of doing the whole page at once I've generally found it more practical to go with an approach more like what you're talking about with the append() statements. Its definitely something where I feel like my approach is a work in progress though, not claiming to have the "right" way.

Kekekela
Oct 28, 2004


edit: never mind, gratuitous

Kekekela fucked around with this message at Jul 21, 2009 around 22:07

Kekekela
Oct 28, 2004


Mercator posted:

You don't have javascript? Feh, you don't get to delete anything.

I'm saving up to buy it when it goes on sale.

Kekekela
Oct 28, 2004


Lumpy posted:

Remember back when this was the jQuery thread?

Php megathread, for those that weren't aware.


edited to provide link without faggoty snarky comment

Kekekela fucked around with this message at Aug 18, 2009 around 20:28

Kekekela
Oct 28, 2004


Hanpan posted:

I am trying to create a really simple image toggle for a "open/close" icon, and after a google it seems you can attach functions to the toggle method. I can't seem to get it to work however:

code:
$('.test').toggle(function(){
             alert("OPEN"); //swap image here
        },function(){
             alert("close"); //swap image here
        });
What am I doing wrong?

You need to put it inside the ready handler, like this...
code:
$(function(){
     $('.test').toggle(function()
     {
         alert("OPEN"); 
     }, function()
     {
         alert("close"); 
     });
 });

Kekekela fucked around with this message at Sep 20, 2009 around 14:20

Kekekela
Oct 28, 2004


On the validator subject, I've been meaning to ask for a slicker way to actually validate a date (since the built in "date" validator is worthless and allows poo poo like 86/87/9999) than what I cobbled together:

code:
jQuery.validator.addMethod(
        "validDate",
        function (value, element) {

            var check = false;
            var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
            if (re.test(value)) {
                var adata = value.split('/');
                var mm = parseInt(adata[0], 10);
                var gg = parseInt(adata[1], 10);
                var aaaa = parseInt(adata[2], 10);

                var xdata = new Date(aaaa, mm - 1, gg);

                if ((xdata.getFullYear() == aaaa) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == gg)) {

                    switch (Number(mm)) {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            check = Number(gg) <= 31;
                            break;
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            check = Number(gg) <= 30;
                            break;
                        case 2:
                            if (isLeapYear(aaaa)) check = Number(gg) <= 29;
                            else check = Number(gg) <= 28;
                            break;
                    }
                }
                else
                    check = false;
            } else
                check = false;
            return this.optional(element) || check;
        },
        "Please enter a valid date."
);

function isLeapYear(y) {
    return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}
I think I pulled the part that parses out the month/day/year from someone else's code since those don't look like my variable names and I suck with regex, but I wrote this a while back so can't really remember.

Kekekela
Oct 28, 2004


MEAT TREAT posted:

Remember that date validation is Locale specific, but there are a lot of good jquery date validator plugins.

All of the dates have to be mm/dd/yyyy, its a preexisting site that handles prescriptions for customers in the US. It already uses jQuery validator so I wanted to just be able to add a custom validation method to that instead of using a separate plugin.

Adbot
ADBOT LOVES YOU

Kekekela
Oct 28, 2004


Lumpy posted:

When I don't roll my own, I use this:
http://bassistance.de/jquery-plugin...gin-validation/

Also what I use. Provides good functionality "out of the box" and very easy to customize with your own rules.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply