How can I alter the background color using Jquery or CSS3?

UPDATE

Update:

After following Zach Saucier's instructions, I added the specified codes:

JS

this.onclick = function() { this.style.background = "red"; this.innerHTML = "Bought"; }

So the button displays as:

<a onclick="javascript:func(this)" class="ajax_add_to_cart_button comprar_mat" rel="ajax_id_product_14" href="../cart.php?add&amp;id_product=14" id="matricularme">Matricularme</a>

Despite these changes, the button still does not work


I am attempting to add some effects on buttons within my website but am facing difficulties.

The HTML link (displayed as a button) appears like this:

<td class="marker">
    <a class="ajax_add_to_cart_button comprar_mat" rel="ajax_id_product_14" href="../cart.php?add&amp;id_product=14">Buy it now!</a>
</td>

This particular button is used to add a product on my website, and I simply want to replace the entire text with "Bought" and modify the background as well.

What approach should I take for this? Should I utilize jQuery? Or maybe implement a CSS style where one sprite image represents "Buy it now!" and "Bought", allowing me to just change the background position?

Thank you

Answer №1

When dealing with HTML code like this

<td class="marker">
    <a onclick="purchase()" id="myid">Buy now!</a>
</td>

Implement this JavaScript function

function purchase() { 
    var element = document.getElementById("myid");
    element.style.background = "red"; 
    element.innerHTML = "Purchased"; 
}

Check out the fiddle here

For a different approach, consider the improved reusability suggested by Zach and how to handle variable strings:

<td class="marker">
    <a onclick="purchase(this)" id="myid">Buy now!</a>
</td>
<br>
<td class="marker">
    <a onclick="purchase(this)" id="myid">Purchase as gift!</a>
</td>

function purchase(element) { 
    element.style.background = "red"; 
    element.innerHTML = element.innerHTML.replace("Buy it", "Purchased"); 
}

See the demo in action

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

What is the best way to refresh an owl-carousel element?

Currently, I am utilizing the owl-carousel-2 plugin for my project. However, I have come across a particular issue: The coding structure: <div class="owl-carousel" style="display: none;"> <div class="item"><img src="..." /></div ...

Unable to locate a resolution for the error message "Warning: Task "uglify" not found"

Below is the content of my Gruntfile.js: module.exports = function(grunt) { require('load-grunt-tasks')(grunt); grunt.initConfig({ uglify: { start: { files: { 'js/script.min.js': ['js/script.js&a ...

The CSS :lang() selector targets elements within documents that do not specify a particular language

Can elements that do not have a language set or inherited, meaning they are in an unspecified ("unknown") language, be targeted? Facts The language of an HTML document or element can be specified using the HTML lang attribute, for example: <html lang=& ...

angular-ui-tab-scroll: Odd spacing between blocks and tabs, each separated individually

Greetings! I would like to express my gratitude for this wonderful library! However, I am encountering an unusual issue when trying to wrap a tabset with tabs that are included separately. This can be done either by adding individual tab elements manually ...

"To enhance the functionality of your website in Chrome, incorporate Bootstrap affix and

When I add a website name as a prefix, the name is initially hidden. As the user scrolls down, the website name becomes visible, and as they scroll back up, it disappears again. I have implemented this feature using jQuery. $(document).ready(function(){ ...

Having trouble displaying API JSON data in a Vue.js table component

In my HTML file, I have implemented fetching data from an API and displaying it in a table successfully. Now, I am trying to convert the HTML file to use Vue.js, but encountering some issues. Despite being able to fetch data from the API with Vue, the tab ...

What is the best way to integrate LESS css into a Reactjs application?

I am looking to incorporate LESS into my React app, but I have been unsuccessful in finding a solution through Google. As a newcomer to ReactJs, I lack deep knowledge and would greatly appreciate guidance on installing Less. If anyone is able to help me ...

Having trouble with Drag & Drop on Safari iOS? It seems that it won't drag or respond to drop on desktop or iPad

As I work on coding a webpage designed for viewing on an iPad, I have encountered an issue with Safari/Webkit's drag and drop functionality. Despite copying Safari's example code exactly, the drag and drop feature refuses to work as expected. Th ...

Styling radio buttons with customized CSS borders

I'm attempting to style some radio buttons with a bold red border, but I'm having trouble getting it to display correctly in the latest versions of Firefox and Chrome. It works fine in IE9/IE8. For each input element that is required on my form, ...

Dealing with h2 text overflowing within a bordered container

I need help creating an h2 element with a double border. Here is my current HTML+CSS snippet: h2.titulo { width: 100%; margin: 10px 0; padding: 10px 0; text-transform: uppercase; font-weight: 400; font-size: 30px; display: block; color: ...

Creating an HTML5 canvas that expands to cover the entire html document

After learning JavaScript, I decided to create a page on Github. Everything seems fine, except that the JS code for canvas doesn't scale to fit the entire HTML page. The particles only bounce off the initial window, leaving empty space when scrolling ...

Change the css code to override the color of the background material

Hey there, I'm facing an issue with overriding the background color of a material element. I've tried several methods but can't seem to get it to change. After inspecting the element on the web browser, I found the CSS code that controls th ...

What is the best way to insert a triangle shape to the bottom of a div with an opacity level set at 0.2

https://i.stack.imgur.com/sqZpM.png I've been trying to create something similar, but my triangle keeps overlapping with the background in the next section. I've already spent 3 hours on it. Any help would be greatly appreciated. Check out my c ...

aligning two elements within a container to have equal heights

I need to position 2 divs inside a container div. Their height should always be the same, regardless of content. To achieve this, I am using absolute positioning with top and bottom set to 0. Currently, the container div #three collapses and hides the cont ...

What steps should I take to address the issue of fixing the classname rather than using the

<div ng-class:"{{myclass}}" role="progressbar" aria-valuenow="{{roundedtotalPerformanceCount}}" aria-valuemin="0" aria-valuemax="100" ng-style="{'width' : ( totalPerformanceCount + '%' ) }"> {{roundedtotalPerformanceCou ...

Issue with top-level navigation menu dropdown functionality

I am experiencing an issue with a dropdown menu that has top level and two sub levels. The problem is that while the sub levels function correctly when clicked, the top level does not navigate to the intended page upon clicking. var menu_Sub = $(".men ...

Is it possible to utilize LESS to dynamically read the width?

My LESS code looks like this: &.settings-prompt .panel { margin-left: -190px; width: 380px; height: 200px; } &.create-playlist-prompt .panel { margin-left: -165px; width: 330px; height: 180px; } I want the .panel to have ...

Guide to animate the appearance of a div from a specific location

I have a div that smoothly slides out when a label is hovered over. HTML: <div class="cellDataViewTdmStatus divCell userSitesCol7"> <label class="lblViewTdmStatus">View Status</label> </div> <div id="tdmStatus" class="hid ...

Tips for enhancing the transition effect of animated gifs on a webpage using JavaScript

In my code, I have an interval set to run every seven seconds. Within this interval, there are two gifs that each last for seven seconds. My goal is to display one of the gifs in a div (referred to as "face") based on certain conditions - for example, if t ...

What is the best way to format an image within an <a> element?

Below is a code snippet that includes a tags, with one containing an image. <div class="container"> <a href="#">Not styled</a> <a href="#"><img src="image.png"></a> </div> If I specifically want to style ...