Search code examples
scalacss-selectorslift

How to select "a.my" as jquery in lift?


I want to select all <a> with class myin liftweb, and set its text to ???:

val x = <div>
           <a><span class="my">xxx</span></a>
           <a class="my">yyy</a>
        </div>

I tried:

"a .my *" #> "???"

and

"a" #> (".my *" #> "???")

But neither works, because both of them convert the x to:

 <div>
    <a><span class="my">???</span></a>
    <a class="my">???</a>
 </div>

Which are incorrect, they should only convert the second <a>.

What's the correct code?


Solution

  • As far as I know, there is no direct way to do that using Lift 2.5 and earlier. I believe they will be adding support for that more robust type of binding to Lift 3, but since it is not here yet you will need to work around it.

    Since you can work directly with the NodeSeq on the right of the CssSelector, something like this should allow you to accomplish what you are looking to do:

    "a" #> { ns:NodeSeq => 
      if((ns \ "@class").text == "my")
        ("* *" #> "???").apply(ns)
      else 
        ns 
    }