Looking to modify the button's background color when it is clicked using either Javascript or jQuery?

I have 2 buttons with class df-button and df-web-design - I want to change the background color of only the clicked button when it is clicked, and then when the other button is clicked, the background should return to its default color. I am looking to achieve this using JQuery, similar to how an active tab item works when clicked.

<script>
$(document).ready(function(){
  $("button").click(function(){
    $(".df-web-design").addClass("df-button-active");
    $(".df-button").removeClass("df-button-active");
  });
});
</script>
.df-button-active {
background-color: green;
color: white;
transition: all 0.5s ease;
}
<a class="df-button" href"#">All</a>
<a class="df-web-design" href"#">All</a>

Since this is a page builder in Wordpress, I am using page CSS or a code snippet like this. Is there something I am missing as it's not working? I am new to JS, so please excuse my lack of expertise. Your help and advice would be greatly appreciated. Thank you in advance.

Answer №1

Simply implement the following code snippet. For more information, refer to this link https://api.jquery.com/click/.

  $(".df-button").click(function(){
    $(this).addClass("df-button-active");
  });
  $(".df-web-design").click(function(){
    $(".df-button").removeClass("df-button-active");
  });

https://jsfiddle.net/e95xrtjL/1/

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

Tips for converting an entire column along the vertical axis to a different language

Looking for a solution with my grid view where I want to translate the items of the second column on the y axis, as shown in the image. I attempted to use the translateY() function, but it resulted in the need to scroll in order to see the translated items ...

Tips for extracting data by scrolling through a webpage that initially displays only 10 items, but dynamically loads additional items as you scroll down

I am attempting to extract data from the provided webpage by collecting information contained within the title div tag. The challenge I am facing is that as I scroll down, more title tags are dynamically loaded onto the page. Initially, only a few brands a ...

Trigger Ajax POST on modal close button click

I am facing an issue with my modal where it still performs an Ajax post even after clicking the 'Close' button. I want to prevent the Ajax post when the user clicks on 'Close'. Any suggestions on how to handle this situation? Modal &l ...

Issues with PHP Form Email DeliveryIt seems that there is a

I currently have a ready-made form that is constructed using html, css, jquery, and ajax. It's basically copied and pasted onto my contact page for now. This is the code I include before the html tag on the contact.php page. <?php session_name(" ...

Utilizing Jquery Ajax calls to populate a user list on project selection

I am currently utilizing Jira and in need of populating the list of users for the selected project. <td><select id="projectsList" class="textFld" multiple="multiple"></select></td> <td><select id="userList" class=" ...

Please provide values in the input box, with each value separated by

I attempted the following code: success: function (result) { for (var i = 0; i < result.d.length; i++) { var emails = new Array(); emails = result.d[i].Emailid; alert(emails); $("#EmailCC").val(emails); ...

Finding dynamic elements with Selenium WebDriver is an essential skill for any automation tester

<div id="button" data-testid="widgetButton" class=" chat-closed mobile-size__large"> It seems there is an issue with locating elements that have special characters in their names. I'm attempting to click on a specific item, but I've tried ...

Is there a way to modify HTML using a C# program in a web browser?

I’m considering the possibility of editing the HTML document text through the web browser. I am currently working on an email client that utilizes an HTML template for its design. Everything seems to be in order, but now I need to customize the template ...

What is the reason behind using AJAX to attempt sending a new URL request on

Having a strange issue with my product list. When trying to edit a product by clicking on it, I am redirected to the product form where an AJAX request is supposed to be sent to getFiltersGroup. However, on error, the AJAX request is somehow being sent to ...

"Encountering an error while attempting to map a JSON array in

I am just starting to work with knockoutjs and I'm attempting to map JSON data to KO. Here is my code: My goal is to parse the JSON data and display it in a table. However, when I try to use ko.compute, I encounter an error saying "self.firstName() i ...

Steps for including a solid bottom border in a toggled navigation bar that is responsive

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document&l ...

Preventing Javascript Pop Up from automatically jumping to the top of the page

Upon clicking a button (refer to image below and take note of the scroll bar position), a div pop up is triggered through Javascript. View image: https://docs.google.com/file/d/0B1O3Ee_1Z5cRTko0anExazBBQkU/preview However, when the button is clicked, the ...

Methods for creating overlapping background images in breadcrumbs

I am having trouble with making the breadcrumb frame overlap the inside images. I attempted using z-index: -1, but it causes the images to disappear. Please refer to the attached image for a better understanding. Thank you. .breadcrumb { margin-botto ...

How can a new <li> element be created without being influenced by certain CSS styles?

I am attempting to enhance my menubar by adding a signup/login item aligned on the right without affecting the other center-aligned items. Essentially, I have an entry named "Login/Sign Up" that should maintain its own unique style while seamlessly blendin ...

Using AngularJS to create a form and showcase JSON information

My code is below: PizzaStore.html: <!DOCTYPE html> <html ng-app="PizzaApp"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Delicious pizza for all!</title> ...

Utilizing REST API calls for querying data in MongoDB

My goal is to send an XMLHttpRequest to mongoDB using AJAX to retrieve a document. This is the code I have written: function getJsonDocumentByModeldid(model_id) { var inputVal = document.getElementById('inputModelId').value; alert(input ...

Utilizing Javascript to set the innerHTML as the value of a form

I have written a JavaScript code that utilizes the innerHTML function, as shown below: <script> var x = document.getElementById("gps"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showP ...

Discover the best method for combining and comparing arrays using jQuery

Currently, I have two arrays: existing_names = username1, username2, username3; new_names = username1, username4, username5; I want my final array to be like this: final_names = username1, username2, username3, username4, username5; Is there a way to ...

Enhancing drag items in jquery UI Sortable with customizable drag images

Is there a method to include a drag image on the item being dragged? I attempted to achieve this using the code below: $('.selector').sortable({ start: function( event, ui ) { $(ui.item).addClass('dragImage'); } }); While t ...

Inserting a vertical barrier in the midst of the content

I am struggling to create a vertical divider and align the text properly in the top right corner of my screen. https://i.sstatic.net/UqURE.png Currently, I am facing issues with displaying the divider and positioning the text correctly. <div class="r ...