1

I have this CSS selector, which just won't bind .live(), but works perfectly with .bind():

$('.treeView li ~ :has("ul")').prev('li')

I've read somewhere, that using the .prev() and .next() could be troublesome coupled with .live(), so I wanted to create an pure CSS selector (or ANYTHING that works with .live()!) to do the job - but I just can't figure it out!

Here's what I want to do: Select all li-elements, which is followed by an li containing an ul.

Here's an HTML markup example, I want to select the ones where the comment says "selected"

<div class="treeView" id="processSegmentsTree">
    <ul>
        <li>Ostetanke</li>    //selected
        <li>
            <ul>
                <li>Tank 1</li>
                <li>Tank 2</li>
            </ul>
        </li>
        <li>Presse</li>    //selected
        <li>
            <ul>
                <li>Sub-leaf 1</li>
                <li>Sub-leaf 2</li>    //selected
                <li>
                    <ul>
                        <li>test</li>
                        <li>test</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

Any help is much appreciated, since this is a bit of a thorn in my eye :(

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • 1
    Well [there is no previous sibling selector in CSS](http://stackoverflow.com/questions/1817792/css-previous-sibling-selector), and jQuery doesn't implement one of its own... – BoltClock Jul 11 '11 at 12:23
  • I am well aware of that (thourght I made that clear :P), I am looking for a way to achieve what I need, and make it work with the `.live()` event system. – Thor Jacobsen Jul 11 '11 at 12:40

2 Answers2

2

There is no such Selector to accomplish this task.

What I highly recommend you to do is to add ANYTHING as a flag to use when selecting your desired elements. add attribute, wrapper, ANYTHING YOU WANT, then the task will be as easy as it should be.

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
  • Yeah, you're probably right. What I was after what a way to add new branches and leafs to a treeView, and not have to worry about re-binding event handlers. But now that I've actually implemented all the re-binding, I see that your suggestion is way simpler than multi-line, hairy CSS-selectors. Thanks for the help! :) – Thor Jacobsen Jul 11 '11 at 12:54
0

This may work: // not css selector

$(".treeView li:has('ul')").prev();
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • The problem is to get rid of `prev()` because it does not work together with `live`. – Felix Kling Jul 11 '11 at 12:26
  • @Thor Jacobsen Hi friend, there is no css selector that can satisfied your aim. jQuery permit us to use css selectors and also it provides us many of its own process to make query on dom/elements which is not accepted in css. – thecodeparadox Jul 11 '11 at 12:29
  • @Thor do u have any chance to generate ul-li dynamically? – thecodeparadox Jul 11 '11 at 12:47