Using JavaScript to link buttons and dynamically change their colors

Just starting to dabble in JavaScript and attempting to change the colors of my website's header, footer, and background. I've created two buttons:

  <div class="styleBut">  
   <a href="#" class="btn btn-warning">ReStyle</a>
   <a href="#" class="btn btn-warning">Reset</a>
  </div>

Here is the JavaScript code I am using:

I am struggling to figure out how to connect this script to my buttons, I assume it involves an onclick event, but for some reason it's not functioning as expected.

Answer №1

<div class="newStyle">  
   <a href="javascript:void(0);" class="btn btn-warning">NewStyle</a>
   <a href="javascript:void(0);" class="btn btn-warning" id="resetStyles">ResetStyles</a>
  </div>

replace # with javascript:void(0); in the href attribute to prevent default link behavior.

JavaScript :

document.getElemenentById("resetStyles").onclick = revertBack

Answer №2

Make sure to assign unique id attributes to your anchor elements:

    <a href="#" id="customize" class="btn btn-primary">Customize</a>
    <a href="#" id="undo" class="btn btn-primary">Undo</a>

Next, utilize JavaScript to access these elements:

    var customizeButton = document.getElementById("customize");
    var undoButton = document.getElementById("undo");

Finally, link the elements to click events and corresponding functions, like so:

    customizeButton.addEventListener("click", customizeFunction);
    undoButton.addEventListener("click", undoFunction);

Answer №3

When implementing this particular syntax, it is expected to function correctly:

<a href="#" class="btn btn-warning" onclick="customizeDesign()">Revamp</a>

Additionally, confirming that the components you intend to modify possess a unique identifier is essential:

<div id="section">

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

Experience the frustration when the Ruby on Rails Open Modal Box Link fails to work. Stay tuned for the latest update on the link

My goal is to have a Modal Popup Box open when a specific link is clicked on my page. Here is the code I added to my view file: <%= link_to_remote '+ Add Discount', :url => {:controller => "parent_wise_fee_payments", :action => "ne ...

Mastering the Art of Defining SVG Gradients in a Page-wide CSS File

On my HTML page, I have implemented a JavaScript code that dynamically generates SVG elements and inserts them into the HTML page. Currently, all the styling attributes such as fill colors are specified in the CSS of the entire webpage. This approach allo ...

Displaying a pair of values using a single noUISlider

I'm trying to achieve something unique with a noUIslider range by outputting two values. While I've managed to display the same value twice using examples from the noUIslider documentation, my goal is to have one of the outputs show a value that ...

Can a function be activated in JavaScript when location permission is declined?

Background: Following up on a previous question regarding the use of getCurrentPosition and async functions. I am currently working on The Odin Project and attempting to create a basic weather application. My goal is to include a feature that automatically ...

Steps for organizing an array based on unique requirements

My goal is to retrieve an array that consists of the first and second objects based on the "point" property of each object I have a specific condition where if it is satisfied, the positions will be swapped. Default: ENGLISH[0] ENGLISH[1] MATH[0] MATH[1] ...

Retrieve the data from an input field in Selenium automation

Despite the text being visible in the input Text Box, I am encountering difficulties capturing it from the html DOM. I attempted to use getAttribute('value'), however, since the value attribute is not present, this method proved unsuccessful. Sim ...

Locating the xpath of an element directly beneath the opening <body> tag

I'm having trouble finding the xpath for an element that is not associated with any html tags. Please see the attached image for reference. I tried using driver.findElement(By.xpath("/html/body/"));, but it's not working. I need to locate the tex ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

Seeking Up/Down Arrows HTML numerical input feature specifically designed for iOS devices

I am having an issue with a number input box on iOS. I am utilizing jquery, kendo mobile, and HTML5 for this particular task. While the number input displays up and down arrows in the Icenium simulator, they are not showing on the iPad. I am seeking a sol ...

Is there a way to prevent the row background color from stretching along with the content of the dropdown accordion?

Is there a way to prevent the row background color from expanding along with the content of the dropdown accordion? I want the row to remain fixed in place and have the dropdown content start below the selected row. Currently, the style of the row changes ...

Unfortunately, the rest operator is not compatible with webpack when using Babel

Currently in my app, I am utilizing webpack and React. However, I have encountered an issue where webpack does not seem to be accepting the syntax var {id, ...data} = pulse;. Error: ERROR in ./src/main.js Module build failed: SyntaxError: Unexpected toke ...

I am having trouble debugging the Twitter Bootstrap v4-alpha grid. The col-sm-offset-* class doesn't seem to be

Something has been altered somewhere, causing col-sm-offset-*s to stop functioning entirely. For instance, when attempting to split the screen in half and only display content on the right side, I typically would use: <div class="container"> < ...

Troubleshooting Issue: Chrome Extension Fails to Send Data via Ajax

I'm encountering an issue with my Chrome Extension where I can successfully retrieve data from my 'punch.php' file, but I am unable to pass data to it. Below is the code snippet causing the problem: jQuery.fn.punch = function(){ $(this) ...

Why is the Javascript code outputting undefined and NaN during execution?

As a newcomer to the world of javascript, I am venturing into learning its fundamental concepts. In my quest for knowledge, I've dabbled in file reading and came up with a small script which you can find below. // Incorporating the fs (filesystem) mo ...

Jest tests are failing because React is not defined

I am attempting to implement unit tests using Jest and React Testing Library in my code. However, I have encountered an issue where the tests are failing due to the React variable being undefined. Below is my configuration: const { pathsToModuleNameMapper ...

"Utilizing Jest Globals to Provide an Empty Input for a Function: A Step-by-

I have developed a function that outputs null when the input is empty: const testFunc = (param) => { if (param) { //blabla } return null; }; To verify the return null behavior with an empty param, I want to utilize describe...it...: describe( ...

Save Scope in JSON format

Currently, I am developing a web application that dynamically displays specific prompts based on the field in focus. There are several different texts stored as scripts that I want to showcase at these different points. My goal is to incorporate scope dat ...

To display the newly received ajax content on the page, I must ensure that the DOM can properly render the new divs

Currently, I have a page that undergoes a lot of ajax operations. The ajax function loads a table of products which contains buttons for updating and deleting items. However, these buttons don't respond unless I include $(document).ready inside the su ...

Incorporating a loading animation with an AJAX call

I've come across various solutions to this issue, one of the most enlightening being a response on Stack Overflow suggesting to "display it as soon as you initiate the AJAX call" and then "hide the image once the request is complete." But how would I ...

Executing asynchronous actions with useEffect

Within my useEffect hook, I am making several API requests: useEffect(() => { dispatch(action1()); dispatch(action2()); dispatch(action3()); }, []); I want to implement a 'loading' parameter using async/await functions in the hook ...