How to add apply CSS using jQuery's closest method?

jquery add css class
jquery change css class
remove css jquery
jquery css multiple
javascript change css class
add remove css jquery
jquery css important
jquery style', 'display

$("p.ABC ").closest(".b .y").addClass("t");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="ABC">A..........Z</p> //this could be present in some pages
  </div>
</div>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="BC">A..........Z</p> //this could be present in some pages
  </div>
</div>

.closest() will not work with the multi-level selector. Hence, closest(".b .y") will not match any element.

You have to use .find() on the .closest() element:

console.log($("p.ABC ").closest(".b .y").length); // 0

$("p.ABC ").closest(".b").find(".y").addClass("t");
.t{
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="ABC">A..........Z</p> //this could be present in some pages
  </div>
</div>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="BC">A..........Z</p> //this could be present in some pages
  </div>
</div>

jQuery css() Method, jQuery Manipulating CSS. addClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements. css() - Sets or returns the style attribute. First create simple html page and inside write down your css. Next you create simple Query and and apply custom css. Now if you want apply which tag or attributes css using JQuery. When I write down this code to easily understand

$('.b h1:first').addClass("t");

jQuery Get and Set CSS Classes, css() method a Javascript object that contains the properties and values as key/​value pairs. This way, each property will then be set on the jQuery  This is a paragraph to style using jQuery css(). You have to click the above button to apply the multiple CSS to the paragraph. It applies as many CSS properties as you want. Answer 2: Using jQuery attr() It applies the multiple CSS by adding the style attribute to the element. This method can also be used to add an attribute in jQuery. You have to write the CSS property in bulk with the style attribute.

You can also use the following:

$("p.ABC ").closest(".a").prev('.y').addClass("t");
.t{
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="ABC">A..........Z</p> //this could be present in some pages
  </div>
</div>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="BC">A..........Z</p> //this could be present in some pages
  </div>
</div>

How to change CSS using jQuery?, Get the value of a computed style property for the first element in the set of For example, if you want to retrieve the rendered border-width , use: $( elem ).css(  jQuery Add style using jQuery attr() jQuery attr() method is used to add attributes to HTML elements. However, you can use this method to add style attributes and CSS properties to HTML elements. See jQuery attr() method to learn the syntax and see below example for adding attribute using attr(). Add Single Style

The below will highlight the title in the first matching div only

$("p.ABC")            // your starting selector
      .closest(".b")  // get the closest b div
      .first()        // only get the first of these
      .find('.y')     // find the title
      .addClass("t"); // add class to the first div's title only
.t {
  color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="ABC">A..........Z</p> //this could be present in some pages
  </div>
</div>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="ABC">A..........Z</p> //this could be present in some pages
  </div>
</div>

.css(), addClass( className )Returns: jQuery. Description: Adds the specified class(es) to each element in the set of matched elements. version added:  Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2.). You can apply as many properties as you like in a single call. Here is the syntax for the method − selector.css( {key1:val1, key2:val2.keyN:valN}) Here you can pass key as property and val as its value as described above. Example

closest() is to find the parent of the element.

Check this one.

$(".b h1:first").css("color","red");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="ABC">A..........Z</p> //this could be present in some pages
  </div>
</div>
<div class="b">
  <h1 class="y">Hello</h1>
  <div class="a">
    <p class="BC">A..........Z</p> //this could be present in some pages
  </div>
</div>

.addClass(), You can use the jQuery css() method to add new CSS properties to an element or modify the existing properties values dynamically using jQuery. Let's try out the  You can do the following: $ ('li#nav').css ('background','green'); $ ('li#nav') selects the list element. Then .css ('background','green') applys green background to the li element.

How to Add CSS Properties to an Element Dynamically Using jQuery, Also, use them to jQuery style elements. By using jQuery CSS methods, you can get and set the CSS properties of the elements inside the DOM. jQuery bug #11173 was about fixing .css and !important in jQuery core. The bug was closed as “won’t fix”. The bug was closed as “won’t fix”. However, that bug’s test case was not as restrictive as the one in this question – the test case did not have an inline !important style it was trying to override.

Discover jQuery CSS: Learn How to Use jQuery Add Style, By just applying the .css() method in jQuery, you can easily list and change the CSS properties of any element or set of matched elements in the  Before jQuery version 1.12/2.2, the .addClass() method manipulated the className property of the selected elements, not the class attribute. Once the property was changed, it was the browser that updated the attribute accordingly.

Setting CSS with jQuery - Jason Arnold, The css() method in JQuery is used to change style property of the selected element. css() method also used to add or change the property for the selected​  The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element. However, shorthand CSS properties (like "background" and "border") are not fully supported and may give different results in different browsers.

Comments
  • $("p.ABC ").closest(".b").find(".y").addClass("t"); Or just $("p.ABC ").closest(".y").addClass("t");
  • @Rayon your second one won't work as y is not an ancestor of p.ABC - you would need prevAll instead of closest
  • @Pete - Correct.. I just lost myself as code was not formatted ;)
  • You can refer https://stackoverflow.com/questions/17006763/css-selector-first-div-within-an-id-or-class