1

I am doing ajax POST and when it succeded I would like to replace current div with div from succeded ajax:

var dom;
var target;

$.ajax({
    type: "POST",
    url: "http://127.0.0.1/participants",   
    data: "action=addparticipant&userid=1",
    success: function (html) {

        dom = $.parseHTML(html);
        target = $(dom).find("#listParticipants").val();

        $('#listParticipants').html(target);                
    }
});

but it seems that target is empty...

and when I debug using firebug, than dom is:

[<TextNode textContent="\n\n">, <TextNode textContent="\n\n">,
div#listParticipants, <TextNode textContent="\n \n \n">,
<TextNode textContent="\n\n">, <TextNode textContent="\n\n">]

Could anyone help me solve this problem?

Regards

0

2 Answers 2

2

You want to use $.filter(), not $.find

target = $(dom).filter("#listParticipants").html()

And as Juan pointed out, you want the innerHTML, not the value.

The reason you need to use filter is your console is showing an array of nodes and not a single DOM element. That means that you need to filter that set.

Sign up to request clarification or add additional context in comments.

4 Comments

Out of curiousity can you please explain why filter is better than find? It seems to be doing the same thing here.
@Spokey Because dom is not an element, but an array of them.
@JuanMendes .find only look for descendant. You can see that all his element are in the root of the array. This is the good answer.
Edited just so I can upvote it now that I see why the OP should use filter
2

The $().val() method is for input fields. See http://api.jquery.com/val/

Also as pointed out by epascarello https://stackoverflow.com/a/25142313/227299, you have a set of nodes, so you need to $.filter, not $.find

If you want the innerHTML, use $(dom).filter("#listParticipants").html()

4 Comments

Not the downvoter, but probably because this will not work. Same reason explained on epascarello's answer.
@Karl-AndréGagnon Thanks, I hadn't realized that the HTML in question didn't have a root node
it is not working in my case. target was still empty. epascarello`s solution works :)
@Hladeo Please describe in detail what you mean by it is not working. What is happening? Errors?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.