Posts | Comments | Email

Using jQuery – javascript for beginners

In this post, we will be looking at some very basic javscript for people new to javascript (and jQuery). Lets get it on!

First thing to learn is how and where to use javascript. Javscript can be placed pretty much anywhere on your page, although it is generally placed in the head section of your document, or after theopening body tag, but this is not a rule.

When writing javascript, we place our script within script tags like so:

<script type="text/javascript">
    document.write("Lets rock!");
</script>

The above will output (write to the browser) “Lets rock!” You will notice that in the opening script tag we declare the script type, in this case (when using javascript): text/javascript. This is a w3c standard, but you don’t really have to do it. But do. It’s good.

So, now we know document.write();, lets learn how to use alert(). The alert() function creates one of those incredibly irritating alert box pop ups. It is a handy (and irritating) way of alerting users of stuff, like input errors and whatnot. It goes like so:

<script type="text/javascript">
    alert("Lets rock!");
</script>

That creates an alert box with the text “Let’s rock!”. Now, lets look at variables in javascript. Variables are a fundamental programming concept. If you don’t know what a variable is, please click here.

Ok, so in javacsript a variable is declared like so:

<script type="text/javascript">
    var name = "Billy-bob";
    alert(name );
</script>

In the above, we are creating a variable (var) called name, who’s value is “Billy-bob”. We are then alerting that variable. Simple? Yes! Now lets try some logic, let’s add some stuff together:

<script type="text/javascript">
    var mynumber = 5;
    var answer = 0;
    answer = mynumber + 8;
    alert(answer);
</script>

You see what we did there? We created a variable called mynumber (which we gave a value of 5). We created a variable called answer (which we gave an initial value of 0). We the redeclared our variable number to equal the sum of our first variable, mynumber, and 8. We then alerted that.

Easy? Yes! That’s it for this very short tutorial. Stay tuned for part 2 where we learn about loops, if and other amazing stuff. We will even start mixing in some jQuery with our boring old javascript. Can’t wait? Neither can I

Share

Comments are closed.