Extract a CSS property and store it in a variable using JQuery

Is there a way to extract a CSS value from a specific class on a webpage and then use it for styling other elements in different ways?

I created an example on jsfiddle where I want to retrieve the background color from

.box

and save it to a variable named "bg".

http://jsfiddle.net/43kt7/

Answer №1

One way to retrieve a specific CSS value using jQuery is by utilizing the css() function. If you want to extract the background-color property, you can achieve this with the following code:

var backgroundColor = $( ".container" ).css( "background-color" );

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

Integrating individual script into HTML

Imagine you have a file named public/index.html. Additionally, there is another html file called otherScript, which contains only <script> tags with a script inside. How can you include these scripts into your public/index.html file? You intend to ...

JQuery is having issues with $(this) and the find function not working properly

I am attempting to access a child of the div with the class form-group, specifically, I want to display the value of the input element. <div class="form-group"> <label>text</label> <input name="text" type="text" class="form-co ...

Dropdown menu in Bootstrap gets cropped behind a scrollable container

I am encountering a problem with my Bootstrap Drop Down. The dropdown is located within a scrollable div with a fixed height. Whenever I click on the dropdown, the menu appears behind the scroll content. However, a normal select works perfectly fine. htt ...

Issue with Vue2's v-text functionality

Recently, I've delved into Vue2 and encountered a problem with form submission and validation on a single page. The issue lies in the error display process – I want errors to be shown beneath each form input as soon as they occur, rather than waitin ...

What impact does reducing the window size have on my header?

While working on a website with a navigation bar, I encountered an issue where the navbar was not responsive. When the window size was reduced, the nav bar shifted to an awkward position vertically, as shown in the first image. The navbar shifts below the ...

What is the best method for transmitting a PHP object via Ajax using JSON format?

Here is the ajax code snippet I am currently using: $.ajax({ type: 'post', url: '/conversation/test', data: { conversation: JSON.stringify(<?php echo json_encode($conversat ...

Searching for a specific element in jQuery by querying a string

I have a situation where an ajax request is made to send text using the POST method to another PHP page. This PHP page then converts the text into markdown format and sends it back. Here's an example of what it looks like: "<p>Meep, meep, <e ...

What is the proper way to apply a background color to the entire body using CSS?

I'm facing an issue with getting the background color to cover the entire body of my webpage. Currently, it only captures a certain size and when there is scrolling on the window, two shades of colors appear. I want the background color to span the en ...

Creating structured paths for retrieving data via HTTP GET requests

In my Laravel project, I have routes set up for requests in a traditional way: Route::get('edit/{user}', 'AdminUserController@getUser'); Route::post('delete', 'AdminUserController@deleteUser'); However, I am facing ...

I am looking to dynamically alter the CSS background URL using JavaScript

Currently, I am looking to update the background image url using JavaScript. Most tutorials I've come across involve having the image downloaded on the desktop and then providing its path. However, in my scenario, the user will input an image link whi ...

Switch between a specific div in a list and conceal the rest

My goal is to have the .information div toggle when the .info div is clicked, while also hiding any other open .information divs. I am currently able to achieve this, but I am facing an issue where an open .information div won't close when clicked aga ...

I selected a single radio button and stored a boolean value (e.g. 1) in the database

I've designed an online exam system where I input questions and various options, as seen in the screenshot below: https://i.sstatic.net/pyCgN.jpg Regarding my questions: I input a question (such as 1+1= ?) and its corresponding options shown in the ...

What is the best way to toggle the visibility of a div using a button? And how can you incorporate a visual effect on the button when

I'm trying to implement a button that can hide and show a div, but for some reason, it only shows the div and doesn't hide it when clicked. Check out my fiddle: http://jsfiddle.net/4vaxE/24/ This is my code: <div class="buttons" style="bac ...

PHP: A guide on validating textboxes with jQuery AJAX

How can I use jQuery AJAX to validate a textbox on focusout? The validation process includes: Sending the value to a PHP page for server-side validation Displaying an alert message based on the validation result I have only impl ...

Difficulty in clearing form values/content despite successfully submitting

Whenever I try to submit my form, the reset function doesn't clear it as expected. Can someone please advise me on how to properly clear my form? My HTML, PHP, and JavaScript codes are provided below. HTML code snippet: <form id="main-contact-for ...

Is there a way to connect the YouTube iframe in order to display a pop-up message?

Is it possible to display popup messages when clicking on a YouTube video embedded in an iframe? <a href='login.html?sid=0&keepThis=true&TB_iframe=true&height=240&width=650' class="thickbox" title='test'> ...

Lines running horizontally on either side of the text

I recently found a helpful solution on Stack Overflow, but it's not exactly what I'm looking for due to two main reasons: I am unsure how to adjust the width of the lines to make them shorter. The solution uses H2, however, I have already d ...

Error: The function 'check' is not defined and cannot be called when the 'on

My aim is to display the "expandrepeat" div when a specific select option is chosen. Unfortunately, the code below doesn't seem to be working as intended: <script> window.check=function(elem) { if (elem.selectedIndex == 1) { documen ...

Prevent accordion expansion triggered by the More button click

When the More button is clicked, it should NOT expand and collapse. https://i.sstatic.net/toV1b.gif If the accordion initial state is open, it must stay open even if button is clicked, if initial state is collapsed then after the more button the accordio ...

Manually adding or removing rows in a DataTable

I've been racking my brain over this issue since yesterday and nothing seems to be working. Here's the problem: I have a table that utilizes DataTables (datatables.net). It needs to be cleared of all rows programmatically and then new rows added ...