0

I been stuck on this for a full afternoon now.

So I have a webshop that displays a product list, rather than post the code here, I will do it conceptually, to simplify the problem. Hoping someone could point me in the right direction.

I have a list of div classes like the following:

<div class="product-container">
    <div class="product-price">Price info</div>
</div>

<div class="product-container">
    <div class="product-price">Price info</div>
    <div class="production-options">
        <select id="selectoptions1" name="product1" class="attribute_list">
          <option value="Colour (please select)">Colour (please select)</option>
          <option value="White">White</option>
          <option value="Navy Blue">Navy Blue</option>
        </select>
    </div>
</div>

<div class="product-container">
    <div class="product-price">Price info</div>
</div>

You will notice that the middle container has a child class production-options. I want to write a JS function that will detect whether a product-container has a child called product-options, If there is, then set the padding of product-price to 20px or whatever.

So javascript would look like this.

if($( ".product-options")) {
    $( ".product-price" ).css( "padding-top", "20px" );
}

Now this will affect all the elements with class name product-price, how do I make it so that it will only affect the class product-price with a sibling product-options? (using ID's is not an option, as these are custom fields/attributes generated by virtuemart).

4 Answers 4

2

Using a combination of filter and next:

$( ".product-price" ).filter(function() {
    return $(this).next(".production-options").length;
});

filter will ensure only product-price elements matching the criteria are returned. next will ensure the next sibling in the DOM has a class production-options. If the product-price can be anywhere (not just directly next) you can use the siblings selector instead:

$( ".product-price" ).filter(function() {
    return $(this).siblings(".production-options").length;
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanx! i appreciate the explanation, i was lost for a while...but this is exactly what i needed! good explanation. In fact they all are :)
1

You can try this code:

$.each($('.product-container'), function() {
    var $this = $(this);
    if($this.find( ".product-options").length) {
         $this.find('.product-price').css( "padding-top", "20px" );
    }
});

1 Comment

if($(this).find( ".product-options")) is always true.
1

One easy solution sis to target the production-options element then find the previous product-price element

$('.production-options').prev('.product-price').css( "padding-top", "20px" );

Demo: Fiddle

Comments

1

Use .parents to select the parent.

$(".production-options").parents(".product-container");

Use .prev to directly select .product-price

$(".production-options").prev(".product-price");

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.