I need to select '#foo #three' using 'this' to get this result:

cat
dog
cat
Html:

<div id="foo">
 <div id="one">cat</div>
 <div id="two">dog</div>
 <div id="three">mouse</div>
</div>

JS:

$("#foo #three").text($('this div:first').text());

I need this code dynamically

http://jsfiddle.net/Kodam/L1eqsj5f/

Recommended Answers

All 3 Replies

Unless you are trying to do this dynamically, this is as simple as:

$('#three').text($('#one').text());

I need to select '#foo #three' using 'this' to get this result

Smells like homework to me?

Well learn what the .text() function is and how to use it, because that's very helpful. Only requires one simple google.

Your code essentially does this: #foo -> #three -> div:first, which of course would only match the following HTML:

<div id="foo">
     <div id="three">
        <div>mouse</div>
     </div>
</div>

I think you need to be clear about what you're trying to achive, why you need to achieve it, and why you need to use this.

Notice how this entire answer could've been found on google if you'd have bothered to look, and then asked an actual question when you can't get it working. Lazy.

Thanks all. My problem is solved now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.