Hello, here is a tutorial on the basics of javascript functions. Here we go...
Before we start, remember to place any codes we make inside a script tag which will look like this:
There are a number of functions in javascript and they can all come in handy when you are creating a webpage. First you start off like this
So far it looks like a CSS code, but don't be fooled! However, I missed something out in this code. Have you guessed? Well if you haven't, I missed out the function name. You can name it whatever you want. For this tutorial, I'll just name it "Hello". So the code will look like this, except you will have replaced the name with your own.
Now between these two squirly brackets (I don't know what they're called
), that is where you place the action. For example if you were going to make an alert box (which I will be in this tutorial), you would put it between the two "squirly brackets". Now we know from my previous tutorial how to make an alert box with javascript, so let's just use it here.
Just remember that you can put any function in there. Well... any function that exists
Now we need to learn how to call a function. For this, I will use a HTML button. You use the onclick action for this. Allow me to demonstrate:
So now, whenever we click on that button, it will display the alert box we created. Here is the final code:
Well done! You just used a function in javascript!
Thanks for reading this tutorial. I'll be writing more soon
Before we start, remember to place any codes we make inside a script tag which will look like this:
Code:
<script type="text/javascript">
</script>
There are a number of functions in javascript and they can all come in handy when you are creating a webpage. First you start off like this
Code:
function() {
}
So far it looks like a CSS code, but don't be fooled! However, I missed something out in this code. Have you guessed? Well if you haven't, I missed out the function name. You can name it whatever you want. For this tutorial, I'll just name it "Hello". So the code will look like this, except you will have replaced the name with your own.
Code:
function hello() {
}
Now between these two squirly brackets (I don't know what they're called
Code:
function hello() {
alert('hello people of the forum!');
}
Just remember that you can put any function in there. Well... any function that exists
Now we need to learn how to call a function. For this, I will use a HTML button. You use the onclick action for this. Allow me to demonstrate:
Code:
<input type="button" value="Click to see the alert box" onclick="hello()" />
So now, whenever we click on that button, it will display the alert box we created. Here is the final code:
Code:
<script type="text/javascript">
function hello(){
alert('Hello members of the forum!');
}
</script>
<body>
<input type="button" value="Click to see the alert box" onclick="hello()" />
Well done! You just used a function in javascript!
Thanks for reading this tutorial. I'll be writing more soon