3

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.

3
  • 1
    if($('.element-rendered .select-inline').length) { $('.element-choice').css('background', 'blue'); } Check updated fiddle 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? 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 Commented Oct 7, 2015 at 14:48

6 Answers 6

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.com/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 recommend you to use class in CSS and add it to the element by using addClass.

Demo

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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.net/SeanWessell/hjpng78s/3/

Comments

1

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.net/hjpng78s/6/

3 Comments

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'll suggest you to use length instead of is, I think .length will be slightly faster than is
@Tushar Added suggestion to use .length for performance instead of .is() to post
0

Demo

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

3 Comments

It doesn't work correctly. The selector in my link doesn't match an element but it still changes the background color.
@Diptox jQuery selector returns an Object, and objects are always truthy, even if the object is empty
my fault , forget the [0] front of the selector
0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.