-3

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?

igr
  • 10,199
  • 13
  • 65
  • 111
M.S.Naidu
  • 2,239
  • 5
  • 32
  • 56

2 Answers2

1

This will work with your exact html above...

$("p").html(function(i, html) {
    return html.replace("{customerDetails}", ci.name + "<br />" + ci.addr1);
});

If there are other paragraph tags in the page then it will run against them as well. As I commented above, we really need more information to give you a good answer, rather than just an answer.

jsfiddle example...

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
0

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?

igr
  • 10,199
  • 13
  • 65
  • 111
  • First of all i would like to thank you for clarifying all the users. I got a solution for query like below, Please let me know if this approach is bad. Jerry doc = Jerry.jerry(FileUtil.readString(file)); String replaced = doc.$("body, p").html().replace("{customerDetails}", "" + ci.name + "" + "
    " + ci.addr1); doc.$("body, p").html(replaced); String processedHtml = doc.html();
    – M.S.Naidu Feb 14 '14 at 14:20
  • That would work (remove comma in CSS query) except it can be more optimized: 1) you are using `replace` that uses regexp - use `StringUtil.replace()` instead; 2) don't lookup twice, assign `doc.$("body p")` to a variable and reuse it. – igr Feb 14 '14 at 17:38
  • Ok i will do that, according to your answer for each 'p' tag will it replace? – M.S.Naidu Feb 15 '14 at 06:22
  • I have only one {customerDetails} in my html, Is it good to loop through all the 'p' in the html file. – M.S.Naidu Feb 17 '14 at 06:03
  • No. In fact, do not parse html at all, just replace token ;) – igr Feb 17 '14 at 14:40
  • can i do more than one replacement using above each method, i have other 'p' tag with '{printBodyHtml}'. – M.S.Naidu Feb 19 '14 at 09:36
  • Sure. Just try to do it with less parsing as possible :) – igr Feb 19 '14 at 11:34
  • but once {customerDetails} is replaced first and it is also replaced while replacing {printBodyTemplate}. Is it good approach? – M.S.Naidu Feb 19 '14 at 13:08
  • Instead i am using doc.$("body"), Please let me know if not good – M.S.Naidu Feb 19 '14 at 14:03
  • Sorry, but that all depends on your use case and your goal, its hard to discuss that like this. In general, you should avoid multiple parsing and try to find the minimal string that contains replacement target. Moreover, this starts to be off topic :) – igr Feb 19 '14 at 14:06