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.
if($('.element-rendered .select-inline').length) { $('.element-choice').css('background', 'blue'); }Check updated fiddle$('.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