How to Give Functionality to a Button With Javascript

by | Apr 6, 2020 | Web Development Tutorials | 0 comments

Javascript gives us the possibility to accessing what we call the DOM, which basically always happens because everything we do in javascript directly or indirectly infers on the HTML or CSS of the site, so there is no mystery in it. And a common way to do it is through what is known as events. And by this, we mean that when an “action occurs” on our website, we can trigger a code sequence. These events can be many, but we are going to focus on the ones that are usually used in buttons, which are onclick, onmouseover and onmouseout.

The same operating logic applies to all of them, so it is enough to explain the most important one (onclick), and the rest can be deduced without much effort.

We are going to do it in just three steps:

First. You must make a button in HTML making sure to give it an id.

<form>

   <input type=button id=myButton value=give me a click!/>

</form>

Second. Create a variable and assign everything that is obtained from that identifier. The complete sentence says: “we are going to access the document obtaining the id (myButton) and those values will be captured in the variable (testButton)”.

<script>

    var testButton = document.getElementById(myButton);

Third. Remember that the variable “testButton” has captured the data of the id. We access that variable and with “addEventListener” we will be attentive to when an action is executed with the button, in this case, the “click” action. Then we add a function, this can be anonymous, it does not need a name, and between its curly brackets, we put the action. In this case, it is just a call to the console, but any function you need could be executed here.

testButton.addEventListener(click,function(){

        console.log(You gave me a click, Well done!);

})

</script>

And we finished. That is all the code that is required.
There is a second alternative and it comes from the very essence of Object-Oriented Programming.
Look at the code:

<form>

<input type=button id=myButtonvalue=give me a click!/>

</form>

<script>

       var testButton = document.getElementById(myButton);

       testButton.onclick = function(){ 

              console.log(You gave me a click, Well done! again); 

}

</script>

As you can see, it’s almost the same code, it continues to use a variable to capture the information of the button by means of its id, and the same anonymous function that executes the call to the console. The difference is in a single line: we access the button property, “onclick”, and that way we avoid using the event.

Interesting!

—————-

We'd love to know your opinion. Leave us a message or follow us! we are on social networks

0 Comments

We have more articles and tutorials that may be of interest to you.

How to Migrate a Website Built In WordPress to the Localhost

This article is aimed for those people who, regardless of the reason, attempt to change their website or their blog developed in WordPress from an external hosting to a local one, localhost, but retaining the content and functionalities of their websites intact. This...

What Elements Cannot Be Missing in a Successful Website?

Creating a successful website requires attention to detail and a deep understanding of what users are looking for. From design to functionality, there are several elements that can not be missing in a successful website. In this article, we'll explore some of the key...

How To Improve The Loading Speed Of Your Website?

In today's fast-paced digital world, having a website that loads quickly is essential. A slow-loading website can have a negative impact on your user experience, leading to decreased engagement and conversion rates. In this article, we'll explore some of the ways you...

Highly recommended books for Marketing Management and Social Media Management:

Knowing the best books for marketing management and social media management is important for improving knowledge and skills, staying up-to-date with the latest trends and strategies, enhancing decision-making, boosting creativity and innovation, and personal and...

The Importance of Having a Website For Your Business

In today's digital age, having a website for your business has become more important than ever before. With the majority of people using the internet to find products and services, having an online presence is crucial to the success of your business. In this blog,...

How to choose the right design for your website?

Creating a website for your business or personal brand is a crucial step in establishing a strong online presence. A well-designed website can help attract and retain visitors, showcase your brand identity, and ultimately drive conversions. However, with so many...

4 Steps to Reach Your Customer

The sales funnel is one of the most influential tools today in digital marketing. It has become an icon of our time and is undoubtedly transforming an entire generation since it has been consolidated for a long time. Since the nineteenth century comes this terminology...

Pay Attention to Your Thoughts

There has been enough talk about the importance of having a positive and focused mind when confronting the different challenges of life, overcoming difficulties, or that such virtue is necessary for the quest of dreams. The scenarios can be extrapolated to themselves...

How to Change The Behavior of The Block Elements in HTML Tags

Sometimes when we’re working with HTML or CSS, there may be the need, depending on the design in mind, to move objects from one side to another, or to add or reduce margins, to align objects next to each other or the one below the other, etc. And as these changes are...

Social Media Tactics

We use the word "tactics" in the title because one must be tactful and not emotional when using social media for business exposure and growth. Social media in its very design is a picker and poker of vanity metrics and the desire for "fame." Did you know there are...
× How can I help you?