What's the issue with this HTML button not functioning properly?

I'm having an issue with a button in my code that is supposed to change the text from 'hello' to 'Goodbye', but for some reason, it's not working. I have ensured that my code is properly indented in my coding program.

Below is the snippet of code causing the problem:

<!doctype html>
<html>
<head>
<title>chat</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<style type="text/css">     
body {
    margin:0px;
}

#topBar {   
    background-color:#33ccff;
    width:100%;
    height:100px; 
    z-index:1;
    margin:0px;
    padding:0px;
    position:fixed;
}

The ID '#logo' represents the div containing the text to be replaced, and '#loginbutton' corresponds to the button triggering the change.

#logo { 
    width:15%;
    height:60px;
    float:left;
    background-color:white;
    margin-left:20px;
    margin-top:20px;
}
#login { 
    width:10%;
    height:60px;
    float:right;
    margin-right:20px;
    margin-top:20px;
    border-radius:8px;
}
#loginbutton { --this is the button that I want to change the text with
    background-color: #lightgrey;
    border: none;
    color: white;
    text-align: center;
    text-decoration: none;
    font-size: 30px;
    cursor: pointer;
    width:100%;
    height:60px;
    border-radius:10px;
    font-family:Impact;
    line-height:60px;
    -webkit-transition-duration: 0.4s;
    transition-duration: 0.4s;
}
#loginbutton:hover {
    background-color: black; 
    color: white;
    box-shadow: 2px 2px 2px rgba(0,0,0,0.24), 2px 2px 2px 2px rgba(0,0,0,0.19);
}
</style>    
</head>

<body>
<div id="topBar">

Here are the specific components involved:

<div id="login">
<button id="loginbutton">LOG IN</button> <!--This is the button-->
</div>
<div id="logo">hello</div> <!--This is the text I am trying to change-->
</div>

This is the JavaScript function I was using:

<script type="text/javscript">
document.getElementById("loginbutton").onclick=function { 
    document.getElementById("logo").innerHTML="Goodbye";
}
</script>
</body>
</html>

Answer №1

You must include the parentheses for the function, as it is currently missing in this instance.

<script type="text/javscript">
document.getElementById("loginbutton").onclick=function() { 
document.getElementById("logo").innerHTML="Goodbye";
}
</script>

Answer №2

It is advisable to utilize an event listener instead of directly adding a click handler.

<script type="text/javscript">
  document.getElementById("loginbutton").addEventListener('click', function(e) {
    document.getElementById("logo").innerHTML = "Goodbye";
  });
</script>

Update

The code provided has a syntax error that may cause it to not work correctly. The correct implementation should include parentheses after the word function, like so:

document.getElementById("loginbutton").onclick=function() { 

Answer №3

This is the correct format:

<script type="text/javascript">
     document.getElementById("loginbutton").onclick = function() { 
     document.getElementById("logo").innerHTML = "Goodbye"; }
  //additional code
</script>

Answer №4

The word text/javscript is spelled incorrectly. Please correct the spelling and remember to insert () after the function word.

Check out the revised code snippet below:

<script type="text/javascript">
 document.getElementById("loginbutton").onclick=function() { 
   document.getElementById("logo").innerHTML="Goodbye";
 }
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Automatically resize and vertically align a centrally positioned div in a designated container

I have a div container that is centered in the middle of another container. I managed to adjust it for one slide, but the issue arises when I create multiple .html files using the same css file - the height of the container does not meet my expectations. I ...

Dynamic SVG circles with timer and progress animation

Is there a way to modify the following: var el = document.getElementById('graph'); // get canvas var options = { percent: el.getAttribute('data-percent') || 25, size: el.getAttribute('data-size') || 220, lineW ...

developing an associative object/map in JavaScript

I have a form featuring various groups of checkboxes and I am attempting to gather all the selected values into an array, then pass that data into an Ajax request. $('#accessoriesOptions input').each(function(index, value){ if($(this).prop(& ...

Displaying a custom error page in Next.js with the appropriate status code

I recently implemented a custom error page following the instructions provided in the documentation. My goal was to use this error page for specific errors that may occur during the getStaticProps function. Here's how I structured it: const Page: Next ...

Create a function in JavaScript that generates all possible unique permutations of a given string, with a special consideration

When given a string such as "this is a search with spaces", the goal is to generate all permutations of that string where the spaces are substituted with dashes. The desired output would look like: ["this-is-a-search-with-spaces"] ["this ...

Is it possible to use cURL to open a particular frame and pass a parameter at the same time?

Currently, I am working on a website with a layout consisting of three frames. The first frame contains two columns, with the second column having two rows. Frame 1 displays a list of countries fetched from a database in the form of URLs. When a country i ...

Encountering a problem with AngularJS ui router templates

I have defined the following routes in my project: $stateProvider .state('access', { abstract: true, url: '/access', templateUrl: 'login.html' }) .state('access.signin', { ...

The touchstart event is activated when you touch an item within

Is it possible to retrieve the index of ul li elements with touchstart, similar to how it can be done with a click function? <ul id = "list" ontouchstart="touchStart(event,'issues')"> <li> ...

How can I add seconds to the jquery datetimepicker plugin?

Looking to implement the datetimepicker plugin from here, but I'm having trouble finding information on how to include the seconds part. Is there a way to add seconds to this plugin? It's a crucial requirement, so any suggestions for another good ...

Utilizing Angular 11's HostListener to capture click events and retrieve the value of an object

Using the HostListener directive, I am listening for the click event on elements of the DOM. @HostListener('click', ['$event.target']) onClick(e) { console.log("event", e) } Upon clicking a button tag, the "e" object contains the fol ...

Unraveling the Mysteries of AngularJS in a Tutorial Snippet

After reading through the enlightening theory snippets in Step 3 of AngularJS Tutorial, one particular passage piqued my curiosity: The scope, which connects our controller and template to create a dynamic view, is not isolated within its own bounda ...

What is the best method for iterating through an array and generating a glossary list organized by categories?

I have an array filled with definitions. How can I use Vue.js to iterate through this array and create a glossary list organized by letters? Desired Output: A Aterm: A definition of aterm B Bterm: A definition of bterm C Cterm: A definition of cterm ...

Press the Button on the Website using an automated script

I am looking for a solution to increase engagement on my website, which has external like buttons from likebtn.com. To incentivize users to utilize the like buttons, I currently have a script in place that randomly assigns likes to various posts. Howe ...

Utilizing offsets/push in the correct manner

I have been developing a Bootstrap website and I wanted to get some feedback on my current code structure. The design is coming along nicely, but I am unsure if this aligns with best practices? <div class="container-fluid"> <div class="row"> ...

A distinct handler function designed for a dynamically generated form

I have 3 MaterialUI TextFields that are rendered n number of times based on user input (stored in a variable named groupMembersCount) in a functional ReactJS component using the useState hook: const [groupDetails, setGroupDetails] = React.useState([ { ...

Issue with Masonry layout not adjusting to change in window size

Something seems to be fixed on displaying four rows, no matter the size of the window. Previously, it would adjust to three rows or fewer as the browser was resized. I recently played around with columnWidth, but reverting it back to 250 doesn't seem ...

Assistance needed with generating unique IDs in MongoDB

Currently, we rely on MongoDB's built-in ids for all of our unique id generation. However, there are specific cases where we must generate an id for an object before adding it to the database. One potential solution is to create an entry, retrieve th ...

JS Function created to supply elements to React component is failing to return correctly

Trying to validate a dataset by checking for specific prefixes or suffixes in a string, and then breaking the string into <span> elements. The current function correctly identifies the relevant morphemes in the data set, but fails to return the split ...

Steps for accessing the value from an input tag within the same form:

Exploring how to utilize the value "typed_from_user" in another tag within the same form, using only PHP & HTML without relying on JavaScript. Is this scenario feasible? For instance, let's say I wish to reuse the term 'pizza' <form> ...

Newbie Query: How can I apply various font weights to a single font within the same stylesheet?

Twice I have inserted the font Acumin Pro Condensed, once with a weight of 400 and again with a weight of 700. I attempted to use both weights separately; assigning the 400 weight to an h3 tag and the 700 weight to an h2 tag. However, only the 700 font we ...