Choose CSS Class Properties

Is there a method in Jquery to target properties of a css class?

example_class{
color: red;
font-size: 30px;
}

<span id="span1" class="example_class">Hello World!</span>
<span id="span2">Second Span</span>

My goal is to extract the color property from span1 and apply it to span2.

Answer №1

I am looking to retrieve the color property of span1 and apply it to span2.

Using jQuery's css method allows us to get the computed value of a property and set it on another element. Here's how:

$("#span2").css("color", $("#span1").css("color"));

For example:

$("#span2").css("color", $("#span1").css("color"));
.example_class {
  color: red;
  font-size: 30px;
}
<span id="span1" class="example_class">Hello World!</span>
<span id="span2">Second Span</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

transform the input button into an anchor tag

How can I create a hyperlink for the code below: echo "<td><input type='button' class='btn btn-link text-dark' value='{$val_str}' onclick='redirect(\"{$value}\")'></td>" ...

Can someone provide guidance on creating a calculator using the Switch statement in Javascript?

Introduction to HTML: <div id="content"> <div id="calculator-container"> <form name="calc"> <!-- The form is named "calc" --> <label for="output">A Simple Calculator</label> & ...

Erase the dynamically loaded page using ajax and conceal the div

Currently, I am utilizing the .on() method with jQuery to display a specific div, and also using .load() to fetch a particular div from a web page hosted on my server. My query is how can I close this div when clicking outside of it, along with removing i ...

A function triggered by the change() method that initiates an AJAX request

Here is an example of code using jQuery to load HTML content via Ajax: <button> Lets Go </button> <select id='first'> <option value='1'> 1 </option> <option value='2'> 2 </option ...

Utilize composer to integrate jquery.js into your project

My attempt to add the jquery.js file to my project using composer has hit a snag. Here is the code snippet I placed in my composer.json located in the root directory of my project: { "type": "package", "package": { "name": "jquery/jquery" ...

Achieving horizontal alignment of list items with jQuery

I've searched high and low on various online forums for a solution to my current problem, but haven't had any luck yet. I suspect that the issue may lie in how I am wording things. http://jsfiddle.net/hzRAN/10/ Here is some example code to provi ...

Is it possible to access the service and 'self' directly from the HTML template?

When working with Angular 6, one method to access component properties from a service is to pass 'self' to the service directly from the component. An example of this implementation is shown below: myComponent.ts public myButton; constructor(p ...

Utilize PHP to input and swap information within SQL databases

TABLE1: table1ID | studentnum | lname | fname | mname TABLE2: table2ID | studentnum | remarks | others //FORM 1 CODE $sql = "INSERT INTO TABLE1 (studentnum, lname, fname, mname) VALUES ('$studentnum','$lname','$fname','$ ...

Tips for integrating Twitter Bootstrap Glyphicons with Wordpress

Despite the numerous questions on Stack Overflow, I have yet to find a solution to my problem. I even went through this Bootstrap 3 Glyphicons CDN Unfortunately, the suggested solution did not work for me. Icons are not showing up on my website , despit ...

Resolution-specific CSS styling

As a newcomer to website development, I have some knowledge of CSS, HTML, and Javascript. However, I am encountering issues with my website. When viewed on low screen resolutions or mobile devices, the right side of my site is cut off, making it impossible ...

spontaneous angular rotations in a constantly shifting CSS environment

Is it possible to apply a random CSS rotation to an image just once, rather than having it continuously spin when hovering over a leaflet map? http://jsfiddle.net/J03rgPf/mzwwfav4/3/ I want the random CSS rotation to be set only one time. How can I achie ...

What is the best way to align this button beside my form field instead of having it appear two lines below?

I am currently in the process of learning Django as I work on creating a website. However, my knowledge of HTML, CSS, and JavaScript is limited. To tackle this issue, I decided to use a website builder to generate a template for me, which I can then modify ...

Adding CSS classes through the .addClass method - A guide on utilizing linked CSS files

I came across this code snippet and I have a quick question. $(window).scroll(function() { var y_scroll_pos = window.pageYOffset; var scroll_pos_test = 150; // adjust as needed if(y_scroll_pos > scroll_pos_test) { $( "#cssmenu" ).addCl ...

What is the best way to exchange the chosen selection within 2 select elements?

I have been trying to swap the cities in my select fields using two select elements, but for some reason, it is not working when the button is clicked: https://i.sstatic.net/mHg4Y.jpg <div class="select-wrapper"> <select class="airport-selec ...

Interactive font-awesome icons within an interactive dropdown menu

I am currently facing an issue with using two Fontawesome icons in a clickable dropdown menu. The dropdown menu does not toggle when I click on the icons directly; however, it works when I click on the padding around them. The example provided by W3schools ...

Trouble with saving the positioning after drag and drop

I am currently facing an issue with my drag-and-drop script where the coordinates of positioned relative divs are not being saved in the same position when I reload. These divs are contained within a container with specific height and width, restricting t ...

An unexplained line break is being added to content loaded via ajax requests

When using either .ajax or .load to load content from an .html page and insert it into an element, I am experiencing a strange issue where a mysterious new line is appended after the content. It is not a break element, but more like a \n or \r ch ...

What is the best way to retrieve a JSON-formatted array through an AJAX call?

I am currently developing a system with two interconnected drop-down lists. The options in the second list (referred to as the 'sensor_list') are determined based on the selection made in the first list (known as the 'node_list'). To ac ...

Pytest is not able to locate any elements on the webpage, yet the same elements can be easily found using the console

When using CSS or XPath in the console (F12), I am able to locate the element on the page. $$("span.menu-item[data-vars-category-name='Most Popular']") However, when trying to find the same elements with Selenium (pytest) using: driver.find_el ...

Tips for adjusting the size and positioning the ng bootstrap carousel in the center

My Angular project is using ng bootstrap, but I'm facing a styling issue. The content doesn't display in the center, rather it appears in the upper left corner: example I would like the content to be centered and wide under the blue headbar. W ...