3

I'm having a hard time doing something that seems really simple. I have a javascript object:

var main = {
    pages : {
        "123" : {
            "content": "<div><p>The div</p></div><nav><p>The nav</p></nav>",
            "foo":"bar"
        },
        "456" : {
            "content": "<div><p>The div</p></div><nav><p>The nav</p></nav>",
            "foo":"bar"
        }
    }
};

I'm trying the following code:

$(function(){
        var p = main.pages;
        $.each(p,function(i,el){
            var c = p[i].content;
            var newc = $(c).find('nav').html('<p>New content</p>');
            //console.log(c);
            //console.log(newc);
        });
        //console.log(p)
});

To make my object look like:

var main = {
    pages : {
        "123" : {
            "content": "<div><p>The div</p></div><nav><p>New content</p></nav>",
            "foo":"bar"
        },
        "456" : {
            "content": "<div><p>The div</p></div><nav><p>New content</p></nav>",
            "foo":"bar"
        }
    }
};

But I'm having a hard time doing it. What am I missing?

I have set up the former example in jsfiddle

I do understand that it's generally not a good idea trying to manipulate strings as dom elements but I have no other choice since the object comes from a RESTful server and it doesn't make much sense to inject the html in the current page for that.

4
  • If I run the above code i get Object 123: Object content: "<div><p>The div</p></div><nav><p>The nav</p></nav>" foo: "bar" 456: Object content: "<div><p>The div</p></div><nav><p>The nav</p></nav>" foo: "bar" - so it does what you want in your question Commented May 11, 2013 at 7:42
  • You have a js object and you are trying to generate the same? can you please clarify? Commented May 11, 2013 at 7:43
  • Look again, the strings are not the same. In the expected object, the html inside the nav tag is different, changing from "the nav" to "new content". Commented May 11, 2013 at 7:45
  • may i know what actually u want it above code. Commented May 11, 2013 at 7:46

2 Answers 2

2

You can actually build the HTML in jQuery without appending it into the DOM.

$(function () {
    var p = main.pages;
    //a temporary div
    var div = $('<div>');
    $.each(p, function (i, el) {
        //append to div, find and replace the HTML
        div.append(p[i].content).find('nav').html('<p>New content</p>');
        //turn back into a string and store
        p[i].content = div.html();
        //empty the div for the next operation
        div.empty();
    });
    console.log(p);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! You saved the day!!
1

Because $(p[i].content) is not one element but two, so based on your code:

var tmp = $(p[i].content);
tmp.eq(1).html('<p>new content');
console.log(tmp.html());

3 Comments

In fact, since I'm getting content inside a loop, I just have one element each time. I didn't test it but I believe it also works since it's similar to Joseph answers. Thanks a ton!
@Robyflc: Put $(main.pages['123'].content) in a console you will find it returns a set with 2 elements, with <nav> as the root element of the second. This will cause the fallow .find('nav') is searching the child nodes of <nav>, so it wouldn't match anything.
I'm sorry. I completely misunderstood what you meant at first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.