admin管理员组

文章数量:1320786

First of all, sorry if this is a simple question. I am new to jQuery and I want to know how can I check if an element exists and if it does, change the css property.

Here is what I mean: I have the following list:

<ul class="element-rendered">
    <li class="element-choice">Item A</li>
    <li class="select-inline">Item B</li>
</ul>

I want to know how can I check if the class select-inline exists inside element-rendered and if it does, how can I change the css background of element-choice to blue?

I created a fiddle to reproduce this example.

Again sorry if this is a simple question but I am new to jQuery.

First of all, sorry if this is a simple question. I am new to jQuery and I want to know how can I check if an element exists and if it does, change the css property.

Here is what I mean: I have the following list:

<ul class="element-rendered">
    <li class="element-choice">Item A</li>
    <li class="select-inline">Item B</li>
</ul>

I want to know how can I check if the class select-inline exists inside element-rendered and if it does, how can I change the css background of element-choice to blue?

I created a fiddle to reproduce this example.

Again sorry if this is a simple question but I am new to jQuery.

Share Improve this question asked Oct 7, 2015 at 14:42 brunoddbrunodd 2,58412 gold badges45 silver badges68 bronze badges 3
  • 1 if($('.element-rendered .select-inline').length) { $('.element-choice').css('background', 'blue'); } Check updated fiddle – Tushar Commented Oct 7, 2015 at 14:44
  • Hey @Tushar thank you again! You are always helping me here! haha! That worked. Would you mind answer that so I can mark as correct? – brunodd Commented Oct 7, 2015 at 14:47
  • $('.element-rendered .select-inline').css("background-color", "red") will (almost) be a no-op if the element is not found in the DOM. You could go without checking the existance at all – Andreas Commented Oct 7, 2015 at 14:48
Add a ment  | 

6 Answers 6

Reset to default 3

You can use .length to check if the element exists in DOM.

$('.element-rendered .select-inline') will select all the elements having class select-inline inside the element with class element-rendered. .length on selector will return the number of matched elements. So, number greater that one, means the element exists. Then you can use .css to set inline styles.

Demo

if ($('.element-rendered .select-inline').length) {
  $('.element-choice').css('background', 'blue');
}
.element-choice {
  width: 100%;
  background: red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<ul class="element-rendered">
  <li class="element-choice">Item A</li>
  <li class="select-inline">Item B</li>
</ul>


I'll also remend you to use class in CSS and add it to the element by using addClass.

Demo

var eR = $(".element-rendered");
if (eR.find(".select-inline").length > 0){
  eR.find(".element-choice").css("color", "blue");
}

This would work for your specific example.

Find the select-line element which is a child of element-rendered. The find all of the sibling elements with class element-choice and apply the css.

$('.element-rendered>.select-inline').siblings('.element-choice').css('background','blue')

http://jsfiddle/SeanWessell/hjpng78s/3/

To check if element exists could use .is() , or as suggested by @Tushar .length

var container = $(".element-rendered");
// alternatively `!!$(".select-inline", container).length`
$(".select-inline", container).is("*") 
&& $(".element-choice", container).css("background", "blue");

jsfiddle http://jsfiddle/hjpng78s/6/

Demo

if($(".element-rendered .select-inline")[0])
    $(".element-rendered .select-inline").css("background-color","red");

How can I check if the class select-inline exists inside element-rendered and if it does, how can I change the css background of element-choice to blue?

if ( $(".element-rendered > .select-inline").length ) {
    $(".element-rendered > .element-choice").css({
        'background-color': 'blue'
    });
}

Docs:

  • Find the number of elements in the jQuery object.
  • Set one or more CSS properties for the matched element

本文标签: javascriptjQuery How to check if an element exists and change css propertyStack Overflow