3

I have inherited the following code snippet:

<div id="topbox">
    <h3>
        <div class="compare_link">Compare Products</div> 
    <input type="checkbox" id="compare_123" name="chkcompare[]" value="123" />
    </h3>
</div>

What I'm trying to figure out is:

I want to find all h3 selectors that have compare_link and HTML checkbox as children and move the checkbox down 10 pixels.

I know I can find checkboxes with jQuery, but can I do this if the sibling is compare_link?

I don't have access to some of the code which is why I can't move the checkbox down myself.

3 Answers 3

4

You don't need jQuery for this necessarily. You can do this with CSS. However, browser support may vary.

h3 > div.compare_link + input[type="checkbox"] {
  margin-top: 10px;
}

If you want to use jQuery it takes CSS selectors. So the following should work:

$('h3 > div.compare_link + input[type="checkbox"]')
Sign up to request clarification or add additional context in comments.

Comments

2

Are you just asking for the selector?

$('h3 > .compare_link + :checkbox')

4 Comments

Good use of direct child selector.
Yes, because once the elements are selected, then I should be able to set the css. What did you mean by your comment about the direct child selector?
Yes, because once the elements are selected, then I should be able to set the css. What did you mean by your comment about the direct child selector?
sorry about the 2x post, I wasn't done commenting. Did you mean ".compare_link + :checkbox"?
0

For the most flexibility use:

$('h3 div.compare_link').siblings('input:checkbox')

This selects all checkboxes that are siblings of divs with the compare_link class. The div with the compare_link class must be descendants of an h3 tag.

The advantage is that this works even the checkbox is move infront of the div or another div is placed in between or if the compare_link div is wrapped in another div etc...

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.