Replacing CSS using jQuery/JavaScript

Recently, I encountered a challenge with a table that had its border set as 1px solid silver from an external style sheet (a .css file over which I had no control).

The CSS code responsible for styling the table was as follows: my_table tbody td { font: 8.5pt verdana, sans-serif; border-top: solid 1px silver; border-bottom: solid 1px silver; overflow: hidden; padding: 0px 3px 0px 2px; }

My goal was to change the border color of the last row in the table to black using jQuery. The approach I initially took was this:

$('#table_1 tr:last').css('border', '1px solid black');

Unfortunately, this method did not yield the desired result. So, my question now is: How can I achieve this using JQuery or JavaScript?

Answer №1

To achieve the desired effect on the TDs, you must focus on them specifically:

$(document).ready(function(){
  $('#table_1 tr:last td').css('border', '1px solid black');
});

This script will wait for the document to be fully loaded before adding a border to the TD elements in the last row.

Update: Considering your CSS syntax, it is recommended to enhance the specificity of the styling. Here's how (using the class name):

$(document).ready(function(){
  $('.my_table > tbody tr:last td').css('border', '1px solid black');
});

If all else fails, you can try

$('table.my_table > tbody tr:last td')
.

Answer №2

Experiment

this.style.cssText = "border: 2px solid blue !important";

or

$('#table_2 tr:last-child td').css('border', '2px solid green !important');

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

Transfer information from one Angular JS page to another pager based on ID

In my use of the mobile angular js UI framework, I am a beginner in angular js and looking to transmit data from one page to another using city id. When a user clicks on a city, the data should be displayed according to that specific city. HOME PAGE: ht ...

jinja2.exceptions.TemplateSyntaxError: instead of 'static', a ',' was expected

My current project involves using Flask for Python, and I encountered an error when running the project from PyCharm. The error message points to line 192 in my home.html file: jinja2.exceptions.TemplateSyntaxError: expected token ',', got &ap ...

Retrieving data from Firebase using JavaScript to extract object details

I've been trying to understand this issue by looking at similar questions, but I'm still stuck. This is the response I get from Firebase: '{"users":[null,{"-JFhOFSUwhk3Vt2-KmD1": {"color":"White","model":"650i","year":"2014","make":"BMW"} ...

Create an event listener for clicking on an image that potentially has a corner ribbon obscuring a portion of it

In my project, there is a div element with an id of items containing several bootstrap cards. Each card includes an image tag and some are accompanied by the following HTML code: <div class="ribbon"><span>Sale</span></div> This co ...

Chrome not responding to ajax call

I am facing an issue with a Marionette view that is making an ajax request. The request works fine on Firefox but Chrome seems to be ignoring it. In Chrome's inspector network tab, there is no indication of the Ajax call being made to the server. How ...

What happens to top level data in Handlebars templates when using #each loop?

Why does the top-level object data disappear when using Handlebars' #each? For example: Using jQuery and Handlebars, $(this).html(Handlebars.getTemplate('summary')({ base_url: "http://www.mywebsite.com/", // this is what I mean by top ...

Best practices for outputting objects within an array using jQuery and JavaScript

Seeking guidance. I am utilizing a unique jQuery calendar plugin (which appears to be uncommon!) that is fully initialized with JS, without any HTML. In order to include events, you must define each event as an object inside an array: events: [ { ...

Issue with editing JavaScript and HTML code

I am facing an issue with modifying my code. I want to make changes so that it not only fulfills its current purpose but also counts from 1 to a specified number. The current code is in Polish and is quite simple. It currently calculates the factorial of ...

Alter the text color once the halfway point is reached at an angle of 175 degrees

I want the text color to turn white when the background color changes to blue after reaching 50% height with a 175 degree angle. Check out this Fiddle .bg { background: linear-gradient(175deg, #e2e2e2 50%, #435289 50%); color:#000; } <div ...

A guide on extracting XML elements containing periods using JavaScript/extJS

Issue with Dot in XML Tag I am encountering a problem with tags in an XML file I am working on. The issue arises from the presence of dots within the tags, such as <tag.state> and </tag.state>. Unfortunately, JavaScript (specifically extJS) d ...

Personalizing plot choices in highchart visuals

I am facing some styling issues with a highchart chart container in Angular that contains a marker, a line, and an area graph. I want to display labels on the graphs without using a tooltip option, but I am having trouble with label positioning. Click her ...

Select a single option from the group to include in the array

I'm currently developing a new soccer betting application. My goal is to allow users to choose the result of a match - whether it's a win, loss, or draw - and then save that selection in a list of chosen bets. https://i.stack.imgur.com/hO3uV.png ...

Searching for row/column labels within a screen showcasing numerous tables

My current task involves working with two tables displayed on a screen. Table 1 contains specific data that I need to verify. The goal is to confirm the presence of the correct column and row headings in each table. I attempted to achieve this using the ...

Turn off Jquery Mobile Ajax Navigation

After countless hours spent poring over API documentation and online forums, I still can't seem to disable the AJAX Navigation feature. CURRENT SETUP <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="htt ...

The syntax for importing JSON in JavaScript ES6 is incredibly straightforward and

Whenever I attempt to write my code following the ES6 standard and try to import a .json file, it ends up failing on me. import JsonT from "../../Data/t.json" //not functioning as expected var JsonT = require('../../Data/t.json'); //works fine ...

What could be causing the element.style.FontSize to not be effective on classes that have been looped through and stored in an array created with querySelectorAll beforehand?

Greetings Stackoverflow Community, Today, I'm facing an issue related to JavaScript and WordPress. I have a JavaScript script named setDynamicFontHeight.js, as well as PHP documents named header.php and navbar_mobile.php, which simply executes wp_nav_ ...

Error: the function document.title is not defined and cannot be executed, occurring on line 31

I am currently working on a notification script using JavaScript, but when I try to run the js, I encounter the following error: Uncaught TypeError: document.title is not a function This is my script: function notification() { $.ajaxSetup({ cache: ...

Is this HTML/CSS navigation bar code suitable for IE7 and tablet compatibility?

I have a total of 15 content pages that can be categorized into 5 subcategories. Although these subcategories do not have their own individual pages due to the lack of unique content for each. A dropdown menu featuring these 5 categories seems like the mo ...

Error: jwt_decode function has not been declared

I'm currently working on a project and I've hit a roadblock while trying to fetch profile information for the logged-in account using a token. Despite using the JWT-decoding library, I keep encountering the issue where jwt_decode is not defined. ...

Clicking on an anchor tag will open a div and change the background color

.nav_bar { background: #c30015; margin-left: 50px; float: left; } .nav_bar ul { padding: 0; margin: 0; display: flex; border-bottom: thin white solid; } .nav_bar ul li { list-style: none; } .nav_bar ul li a { ...