Search code examples
jqueryjquery-selectorsjoddjodd-lagarto

Replace text with {} symbol in Jerry?


I have the following code:

              <p>
                To
                <br>
            {customerDetails}
              </p>

I tried by following but it gave an error.

    Jerry doc = Jerry.jerry(FileUtil.readString(file));
doc.$("#customerDetails").text(ci.name + "&lt;BR/&gt;" + ci.addr1);

Here the text which will be replaced by {customerDetails} should be independent, thats why i do not want to take any help of tags. If i give any tag for {customerDetails}, "To" will be distrubed.

I want to replace the "{customerDetails}" with some text without take any help of html tags or css classes and id's. Is it possible?


Solution

  • First of all, this is not a javascript question, but Java question. It's about Jodd Jerry, parsing library in Java that looks very similar to jQuery. So all jsfiddle solutions here does not help, and I believe some negative scores on this question are not ok.

    Anyway, the @Archer solution is one that can you give a good direction. Here is the java version of the same:

    Jerry doc = Jerry.jerry("<p>to<br>{customerDetails}</p>");
    
    doc.$("p").each(new JerryFunction() {
        public boolean onNode(Jerry $this, int index) {
            String innerHtml = $this.html();
            innerHtml = StringUtil.replace(
                innerHtml, "{customerDetails}", "Jodd <b>rocks</b>");
            $this.html(innerHtml);
            return true;
        }
    });
    
    String newHtml = doc.html();
    assertEquals("<p>to<br>Jodd <b>rocks</b></p>", newHtml);
    

    Jerry still does not have html(function) type of method implemented (it's on todo), so you have to use each explicitly.

    Note that you can not search for #customerDetails as you don't have any DOM element with this ID. What you could do instead, is to have an empty placeholder div or a span like this:

    <p>to<br><span id="#custdet">{customerDetails}</span></p>
    

    and then you would be able to do something like you wrote in the question.

    Anyway, I would try to solve this in a different way, if possible. If you already have a marker and HTML content, can you simply replace it without parsing?