views:

70

answers:

2

At the moment I'm learning jQuery and I hit the topic about if/else statements. As I have no background in programming this topic is something that I need to practice a bit more to get a thorough understanding of it.

The book I'm studying gave me the advice of just writing different blocks of if/else statements. I just had an idea and wanted to know if its valid:

$(morningWakeup).ready(function() {
    $('#arms').reaction(function() {
    if($'#kid').is(':nagging')) {
        $('#kid').slap();
    } else {
        $('#kid').hug();
    }
 });
});

Let me make it clear that this is a joke of course, but I want to know if this is valid code and if you can supply me with some more examples? Thank you!

+4  A: 

The basic form is perfectly fine, though you've misplaced some parentheses on this line: if($'#kid').is(':nagging')) {. It should be if ($('#kid').is(':nagging')) { instead. Also, note that you'll have better luck setting $('#kid').attr('behaving') to true if you just ignore() him/her for a while instead of slap()ing them. Negative reinforcement sucks. :)

bcat
Ah yes I missed an ( at the beginning, thanks for pointing that out. And also much gratitude on the negative reinforcement ;)
Wollen Muts
Haha, no problem. Also, @Pekka's advice is very good: you'll learn a lot about JavaScript and jQuery just by practicing on some real HTML.
bcat
The book I'm reading right now is from Sitepoint: Learning jQuery: From Novice to Ninja and its filled with real HTML examples. So I'm good there and when I'm trough with the book I have many ideas of using jQuery :)
Wollen Muts
+3  A: 

You're mixing up Javascript and jQuery here: The if/else is basically valid, but the jQuery part (.is etc.) will strongly depend on whether the DOM elements exist, whether they have that property etc.

I would recommend starting with real live HTML to go along.

That, and of course the syntax error @bcat points out...

Pekka
I was under the impression that jquery is javascript.
Ash Burlaczenko
@Ash yup, that's my point: if/else constructs are JavaScript; `$()` and `.is()` and the plethora of helper functions are jQuery.
Pekka
Of course my example can only be fired if the DOM elements exist (and there is a property attached), thanks for pointing that out. My brain came up with this example in a split second. It does that often to get an understanding of things, but you are right when it comes to live HTML. It would make more sense in a practical way.
Wollen Muts
So `$()` and `.is()` are jQuery specific?
Wollen Muts
@Wollen yes, `$()` is a helper function defined by jQuery (an alias to `jQuery()`) and `.is()` is a method of a DOM element extended by jQuery.
Pekka
I read about the `$()` which is an alias to `jQuery()`, but this is only for jQuery and not for other JS library's?
Wollen Muts
@Wollen other libraries like Prototype also use `$` as a shortcut to *their* DOM extension functions. Which is one of the reasons they are not easy to make work together on one page.
Pekka
So say for example that one would like to use 2 different JS library's on one page (site), they can't use the `$`? Or for one library the `$` and the other the regular alias?
Wollen Muts
@Wollen usually the latter: that's the way jQuery's compatibility plugin works.
Pekka
Thanks Pekka :)
Wollen Muts