0

New to JQuery and I've searched the net for topics on traversing. However most discuss scenarios where you are traversing through the current document.

My case is I'll be receiving an html div string via AJAX. Let's say this div:

<div>
    <ul>
        <li id="myValue">HELLO</li>
    </ul>
</div>

Let's say this string is stored in a variable myVar. How can I extract the li with id="myValue" and append it to another element on my current document. Most of the examples I see use $(#idHere). Can I do something like this? $(myVar).$("#myValue").appendTo($("#someOtherElement")?

3 Answers 3

2

You almost have it:

$(myVar).find("#myValue").appendTo($("#someOtherElement"));

Or even this:

$(myVar).find("#myValue").appendTo("#someOtherElement");

since appendTo can take a selector.

Demo: http://jsfiddle.net/ambiguous/qDzBh/

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

2 Comments

How come I can't click the check thing?
I use firefox 7 and nothing happens when I click the check. I'll try when I get home
0

You could do:

$(myVar).find("#myValue").appendTo($("#someOtherElement"));

Comments

0

This would it it probably,

$(yourResponseString).find("li[id=myValue]").appendTo($("#someOtherElement"));

Comments

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.