I'm tired of dealing with text transformations, Javascript, and other shenanigans happening when I least expect it

Recently, I've been experimenting with button hover effects and some basic JavaScript. Unfortunately, I've encountered a few issues along the way.

Below is the code snippet:

document.querySelector('.btn').addEventListener("click", myfunction());

function myFunction() {
  alert("HELLO!");
}
#btn {
  width: 150px;
  height: 100px;
  box-sizing: border-box;
  margin: 10px;
  background-color: #3498db;
  border: 15px outset #2980b9;
  color: #2c3e50;
  font-family: 'Oswald', sans-serif;
  font-size: 25px;
  font-weight: 700px;
  text-tranform: uppercase;
  transition: all .5s;
}

#btn:hover {
  border: 10px outset #2980b9;
  color: #ecf0f1;
  font-size: 26px;

}
<link href='http://fonts.googleapis.com/css?family=Oswald:700' rel='stylesheet' type='text/css'>


<button id="btn" type="button">Click Me</button>

<button id="btn" type="button">Click Me</button>

<button id="btn" type="button">Click Me</button>

<button id="btn" type="button">Click Me</button>

The primary issue I'm facing is that the text-transform property doesn't make the letters on the buttons uppercase as intended.

Secondly, when hovering over the buttons, there's a slight layout adjustment causing them to jump by 1 pixel. I've spent quite some time trying to troubleshoot this, but it remains elusive.

Lastly, my JavaScript function isn't working. Given my limited experience with JS, it's likely due to a small syntax error, but I'd appreciate any guidance in resolving it.

Thank you for your help!

Answer №1

There were a few typos in the code - "text-transform" was misspelled, and there was a difference between "myFunction" and "myfunction" (with a capital F).

It's important to note that you should not include brackets when using the addEventListener method as you want to register the function and not invoke it immediately.

The jiggling effect caused by animating the font-size property can be avoided by refraining from doing so. If necessary, consider using "transform: scale" instead to scale the text smoothly without losing quality.

Avoid using duplicate IDs; switch to using classes for styling purposes.

Keep in mind that querySelector() only returns the first matching element, so if you wish to add event listeners to multiple elements, utilize querySelectorAll() and iterate through the list. This is more common in jQuery syntax.

Here is the updated version of the code:

var buttons = document.querySelectorAll('.btn'), i;
for (i = 0; i < buttons .length; i++) {
  buttons[i].addEventListener("click", myfunction);
}

function myfunction() {
  alert("HELLO!");
}
.btn {
  width: 150px;
  height: 100px;
  box-sizing: border-box;
  margin: 10px;
  background-color: #3498db;
  border: 10px outset #2980b9;
  color: #2c3e50;
  font-family: 'Oswald', sans-serif;
  font-size: 25px;
  font-weight: 700px;
  text-transform: uppercase;
  transition: all .5s;
}

.btn:hover {
  border: 15px outset #2980b9;
  color: #ecf0f1;
  /*font-size: 26px;*/
}
<link href='http://fonts.googleapis.com/css?family=Oswald:700' rel='stylesheet' type='text/css'>

<button class="btn" type="button">Click Me</button>
<button class="btn" type="button">Click Me</button>
<button class="btn" type="button">Click Me</button>
<button class="btn" type="button">Click Me</button>
        

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

Ways to observe redux action flow within a component

I am currently developing a React application structured with the following elements: redux typesafe-actions redux-observable My query is: How can I trigger a UI action when a specific redux action occurs? For instance, if we have these asynchronous ac ...

Is it possible to dynamically insert a ng-mouseover in AngularJS using Javascript?

It seems like there is an issue with this code: var run_div = document.createElement('div'); run_div.className = 'whatever'; run_div.textContent = 'whatever'; run_div.setAttribute('ng-mouseover', 'console.log(&b ...

Issues with updating table data when clicking the "Remove Selected" button a second time in Angular 8

Whenever I click the "Remove Selected" button on my table, the selected rows with checkboxes should be deleted. The issue is that it works perfectly fine the first time I click the button, but if I repeat the same steps (click the checkboxes and then click ...

Varying heights based on the screen size

Currently, I am in the process of designing my website and incorporating some wave elements to enhance the background. However, I've encountered some issues when resizing the screen. Specifically, the waves seem to shift with a space between them as t ...

Demonstrating how to showcase information from an API array in a Node.js application and render it as a

I am aiming to showcase information retrieved from the API on my pug page, specifically displaying the names of car parks. Below is the code from Index.js var request = require('request'); var express = require('express'); var ...

Using JQUERY to execute a callback function after an AJAX request is completed

I encountered a situation where I have the following code snippet: <a onclick="$('.element').hide(0); doMyMethod(_element = $(this));"></a> The doMyMethod() function is actually an ajax request. What I am trying to accomplish is to ...

Unable to sign out user from the server side using Next.js and Supabase

Is there a way to log out a user on the server side using Supabase as the authentication provider? I initially thought that simply calling this function would work: export const getServerSideProps: GetServerSideProps = withPageAuth({ redirectTo: &apos ...

Iterate over the array elements in React by using Hooks on click

I am facing an issue with loading objects separately from a JSON file when a button is clicked. The problem occurs when the index goes out of bounds, resulting in a TypeError "Cannot read property 'content' of undefined" message. I have tried u ...

Stellar Currency Swap

I'm having trouble updating currency exchange rates using the select tag and fetching values with jQuery. Initially, I planned to use {{#if}} from Meteor handlebars to handle the logic. I intended to switch currency fields using MongoDB when the user ...

Anchoring HTTP headers in HTML tags

Currently, I am working on some code to enable dragging files from a web app to the desktop by utilizing Chrome's anchor element dragging support. The challenge I am facing is that certain file links require more than a simple GET request - they nece ...

Find the current elapsed time using Wavesurfer in real time

I am currently utilizing the waveSurfer library created by katspaugh for playing audio files. In order to display 'elapsed time / total time', I have written code in the following manner: waveSurfer.on('play', function() { $scope.g ...

Can the same form be submitted with two different actions?

I am facing an issue with a form that is supposed to submit data to 2 different pages using the POST method. After trying some javascript code, I found that one form submission works correctly while the other does not. <form id="add"> <input ...

Button form featuring Ajax in PHP

Can someone please help me figure out what I am missing here? This is a simple HTML file with a "Create" and "Destroy" button that are supposed to call an API. The Ajax functionality seems to be working, but the PHP function is not getting executed. test ...

Updating the column data in dataTables dynamically

http://example.com/unique-link t = $('#table').DataTable(); $('tbody tr').click(function() { $(this).find('td:last').text('B'); //retrieve data var tr = $(this); var row = t.row(tr); // successfully fetched ...

Dividing a string into an array and displaying it in a table using Laravel

Retrieving a string from the database and using the explode function to split the values. Below is the code snippet: $data = DoctorRegistration::select('products') ->where('doctorid','=',$doctorid) ->get(); ...

What is the method to showcase the value stored in the "username" table?

I came across this code online and attempted to include a column with the "username" value. However, I am facing an issue where it is not displaying the username value from the database in the PHP table. <?php $no = 1; $tampil = mysql_query("SELE ...

Using the <li> element as the primary container for a series of <li>'s within a <ul> is

For my form requirement, I am utilizing Jotforms. After downloading the code, I am currently in the process of customizing the CSS and HTML to fulfill my design needs. Jotforms uses a set of ul , li for managing its HTML structure. However, I encountered ...

The onsubmit button is designed to function with only one specific function

When working with two JavaScript functions, only one error is being displayed instead of both. If I remove one function, the other works fine. But when both functions are present, only one error message is shown. Below is my JavaScript code: functi ...

Is it possible to show more than five buttons in an Amazon Lex-v2 response display?

Here is the sessionState object I am working with: { "sessionAttributes": {}, "dialogAction": { "type": "ElicitSlot", "slotToElicit": "flowName" }, "intent": { "name": &q ...

Using an image for the axis in Wijmo BarGraph

I am currently working with Wijmo barcharts and attempting to create a graph that uses images instead of labels on the x-axis. The code I have at the moment displays the image source as a string rather than displaying the actual image. Does anyone know ho ...