Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Show text after clicking a button

I got this h1 tag that I want change when clicking a button.

I want it so then when you click a button, an h1 tag will change to something else, like to "bananas" or something. How could I use an onclick event to change it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use (addEventListener) with (click) event to listen on click button by using javascript code. i hope it's clear and helpful, Thanks!.

<!DOCTYPE html>
<html>

<head>
    <title>Page Title</title>
</head>

<body>

    <!-- My Heading -->
    <h1 id="MyHeading">This is a Heading</h1>
    <!-- My Button -->
    <button id="MyButton">Click Me!</button>

</body>

</html>

<script>
    //Select button by id
    const MyButton = document.getElementById('MyButton');
    //Add on click listener for button
    MyButton.addEventListener('click', function() {
        //Select (h1) heading by id, and then change it's value to (bananas)
        document.getElementById('MyHeading').innerText = "bananas"
    })
</script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!