Javascript Basics : Part 2 (Functions)
Suppose I want to change the button color when it is clicked. And to do this task, I don’t want to send the page to the server. So, what should I do? Well, HTML DOM has given us some events to work with. We will use one the events, Click event and put some code on its event. The below code will change the button color if clicked on that. Now, if we want to add another button and apply same functionality with the click, the code will like below But if you look closely, we are repeating the code. This is the worst smell of the code smells . So, we should create a method and call that method from the click event. So, how do we create a function in Javascript? Its easy. As we did before, we must add the ‘script’ tag first. Inside of this tag, we will write the function. In javascript, we must write the word ‘function’ before the name of the function. For example: function MyFunctionName() { } Then we will call that function from the event. So, if we refac...