Enhancing a collection of HTML buttons

I am trying to modify an array of HTML buttons using a JavaScript function called getButtons:

function getButtons() { // This function generates buttons displayed in the selection phase.
  var trialButtons = [
  '<button class="jspsych-btn" style="color:blue; font-size: 20px; background-color:#CD6155; border-width: 2px; position:absolute; left: 120px; top:20px;  width:210px; height:170px" >%choice%</button>',
  '<button class="jspsych-btn" style="color:blue; font-size: 20px; background-color:#D7BDE2; border-width: 2px; position:absolute; left: 120px; top:220px; width:210px; height:170px" >%choice%</button>',
  '<button class="jspsych-btn" style="color:blue; font-size: 20px; background-color:green;   border-width: 2px; position:absolute; left: 120px; top:420px; width:210px; height:170px" >%choice%</button>',
  '<button class="jspsych-btn" style="color:blue; font-size: 20px; background-color:#F4D03F; border-width: 2px; position:absolute; right:120px; top:20px;  width:210px; height:170px" >%choice%</button>',
  '<button class="jspsych-btn" style="color:blue; font-size: 20px; background-color:#DC7633; border-width: 2px; position:absolute; right:120px; top:220px; width:210px; height:170px" >%choice%</button>',
  '<button class="jspsych-btn" style="color:blue; font-size: 20px; background-color:#BDC3C7; border-width: 2px; position:absolute; right:120px; top:420px; width:210px; height:170px" >%choice%</button>',
  '<button class="jspsych-btn jspsych-btn_instructions" style="position:absolute; left:180px; bottom:30px;" >%choice%</button>'
  ];
  myButtons = [];
  myButtons.push(trialButtons);
  return myButtons[myButtons.length -1];
}

I am wondering how I can update my button styles across all buttons. Specifically, is there a way to change the border-color property of all buttons to red?

Answer â„–1

var trialButtons = [
  '<button class="jspsych-btn" style="left: 120px; top:20px;" >%choice%</button>',
  '<button class="jspsych-btn" style="left: 120px; top:220px;" >%choice%</button>',
  '<button class="jspsych-btn" style="left: 120px; top:420px;" >%choice%</button>',
  '<button class="jspsych-btn" style="right:120px; top:20px;" >%choice%</button>',
  '<button class="jspsych-btn" style="right:120px; top:220px;" >%choice%</button>',
  '<button class="jspsych-btn" style="right:120px; top:420px;" >%choice%</button>',
  '<button class="jspsych-btn jspsych-btn_instructions" style="left:180px; bottom:30px;" >%choice%</button>'
  ];


  // MAKING SOME STYLISH CHANGES::
  trialButtons = trialButtons.map(each => each.replace('style="', 'style="border-color:red; '))
  
  console.log(trialButtons)

  // REVERSING THE STYLE CHANGES MADE EARLIER::
  trialButtons = trialButtons.map(each => each.replace('border-color:red; ', ''))

  console.log(trialButtons)

Answer â„–2

Instead of simply typing out text for buttons, consider creating actual button elements using JavaScript:

const newButton = document.createElement("button");
newButton.innerHTML = "Click me";
newButton.style.backgroundColor = "#CD6155";

You can then easily customize the button further by adding styles like this:

newButton.style.borderColor = "red";

Answer â„–3

If you want to write code similar to this and style it using CSS, you can do the following:

.custom-btn {color:green; font-size: 18px; background-color:#3498DB; border-width: 3px; position:relative; left: 50px; top:10px; width:200px; height:150px)

To change any values, use the following code snippet:

document.getElementsByClass("custom-btn").style.THINGYOUWISHTOCHANGE = CHANGE; 

If you want to assign unique values to each button, give them their own IDs like so:

<button id = "UniqueID">

You can then change the styles individually or collectively as shown below:

document.getElementsByClass("custom-btn").style.borderColor = 'purple'; 
document.getElementById("UniqueID").style.fontWeight = 'bold';

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

Issue with AJAX/JavaScript causing an alert to not pop up

I am currently studying AJAX/JS and have set up a form submission that triggers an AJAX POST request to retrieve data. The data is returned successfully, however, I am facing an issue with displaying an 'alert' message. Instead of showing the ale ...

Using globs to specify files for gulp.src

I'm facing a challenge in setting up a glob array for my JavaScript concatenation build task within the Gulp workflow. The file structure I am working with looks like this: ├── about │ └── about.js ├── assets ├── contact â ...

What is the best way to prevent a function from running unnecessarily multiple times?

Whenever I click "About Me" multiple times, the jQuery animation stops as expected. However, if I click "My Portfolio", "Web", "Graphic", and "Others" more than once, the #box1 div keeps moving down endlessly. This might not be noticeable at first, but onc ...

Assign the initial pointer of a 2D array to another pointer

As a newcomer to C, I may not be using the correct terminology when it comes to memory allocation. For example, let's say I have a function like this one, which returns a pointer to a pointer of integers. int **foo(){} //The functionality of this fun ...

What is the best way to adjust the value of largePageDataBytes in Next.js?

I am looking to modify the largePageDataBytes setting, despite knowing it may impact performance. I made an attempt in next.config.js with the following code: /** * @type {import('next').NextConfig} */ const nextConfig = { /* config options h ...

Is it advisable to use an autosubmit form for processing online payments?

Situation: In the process of upgrading an outdated PHP 4 website, I am tasked with implementing an online payment system. This will involve utilizing an external payment platform/gateway to handle transactions. After a customer has completed their order ...

What is the process of moving items from one object array to another in C++?

I've been working on a project that involves transferring objects from one array to another, but the code I wrote doesn't seem to be working as intended. In this project, 52 card objects are supposed to be created in one array and then distribute ...

Guide to transforming Excel formulas into JavaScript

In my Excel sheet, I have the following formula: =IF($F6=0,"",IF(I6=0,"",$F6/I6)) where F6=7000 and I6 is empty. The Excel result is displaying no data for the formula. Now, I need to convert this formula into JavaScript. function AB6(F6) { var ...

Passing a JavaScript variable to PHP through AJAX seems to be a difficult task

Currently, I am attempting to retrieve the user's current location. I have a JavaScript script that fetches the current Latitude and Longitude using AJAX to send those variables to index.php. $(document).ready(function() { if ("geolocation" in navig ...

Is there any specific reason not to use a short HTML/CSS syntax like <div a b c></div>?

When it comes to writing HTML in less standard environments, such as a phonegap app where strict syntax and semantics are not crucial, I have developed a unique approach that works for me. I aim to keep my HTML code short and concise by using the followin ...

Tips for sending arguments up in Angular2

I need to supply an argument to a function. <select class="chooseWatchlist" (change)="updateWatchlistTable(item.name)"> <option *ngFor="let item of _items">{{ item.name }}</option> </select> It's crucial for me, as I have ...

React-navigation installation in React Native project failed due to ENOENT error - file or directory does not exist

Encountering errors during the installation of react-navigation in my react native project using npm install @react-navigation/native https://i.sstatic.net/uKiPQ.png The installation process reaches halfway, pauses for a few minutes, and then displays an ...

Identify and react to an unexpected termination of an ajax upload process

My ajax uploading code can determine if a file was successfully uploaded only after the upload has completed. If the upload is terminated before completion, no data is returned. Is there a way to detect when an upload is unexpectedly terminated and alert t ...

I encountered an issue where the data I passed to a component ended up being undefined

So here's the scenario: I'm working on a Next.js project where I have a context for modals. In this context, I store modal details in an array called modalBase. Additionally, I fetch data from another context (toolsContext) to pass it to componen ...

Why won't my Laravel AJAX request work after refreshing the page?

In my Laravel CRUD web application, users can view and edit records with a sorting feature that asynchronously sorts the main records view using AJAX. Everything functions smoothly until a user clicks on a record to edit it. After being redirected through ...

User controls and the window.onload event handler

Is there a way for ASP.NET ASCX controls to have their own individual client-side load event, similar to a window.onload, allowing me to hide loading divs and display content divs once the HTTP transfer is finished? I'm struggling with implementing l ...

Conceal an element using a transition effect, initiating its positioning at the center instead of the default

One of the challenges I'm facing is with a video element that I want to hide gradually when clicking on another element. The issue is that after the animation completes, the video ends up hidden at the top-left corner of the screen instead of at the c ...

What are the distinctions between allocating memory for local and global arrays in the C programming language?

I just finished creating a program to check for array out of bounds errors in both global and local arrays. Scenario 1: /* Demo on Local and Global Array Out of Bounds */ uint8 problematic_array_global[5] = {22,34,65,44,3}; uint16 adjacent_array_global[3 ...

Changing the Angular form based on the value selected from the drop-down menu

I am currently working on a Reactive form in Angular 7 that includes 2 dropdowns (select) which are supposed to function as cascading dropdowns. However, I am facing an issue where the cascading dropdowns are not working as intended. Whenever I select an o ...

Simulation of loopback session

Currently, I am utilizing loopback in conjunction with express session to store cartId. However, for the purpose of making my tests function properly, it is essential that I inject cartId into the request session. Within my remote method, I have implemen ...