DEV Community

Edison Ade
Edison Ade

Posted on

2

Learn about JavaScript Functions

What are functions?

Basically a set of codes in blocks that are reusable, and tells your application to perform a specific task once we call the function name.

Let us create one:
First create an HTML document to display what we create. In the head of your html document.


(<head>

    <title>Website</title>
    <script src="script.js" defer></script>
</head>
 Then create a button in the body of the document
"button onclick = "clickMe()"
<body>
 <button onclick="clickMe()"> CLICK HERE</button>
 <div id ="click">

 </div>
</body>)


Enter fullscreen mode Exit fullscreen mode

Save this as index.html

Then we create the function in another file. Let us call that script.js

//basic function that allows you to click on a button and then display text when we click on the button


function clickMe(){
    me = document.getElementById('click')
    me.innerHTML = "SEE ME HERE"

<code>} </code>

Enter fullscreen mode Exit fullscreen mode

Save the file. Once you click on the button the text should appear.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (2)

Collapse
 
vasilevskialeks profile image
Aleksandar Vasilevsk

Great post, I have written similar post week ago feel free to check it here
codespot.org/javascript-101-functions

Collapse
 
buzzedison profile image
Edison Ade

Oh cool. Will check it out. Thanks.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay