1

I have a div

<div class='text'>
    <span class='s'>Some text</span>
</div>
var div = $('.text');

Now I want to change the text of the span. What I tried is

var span = $(div).find('span.s')[0];
$(span).text('some other text');

But I don't have any idea How to change this inside the var div

3 Answers 3

2

Your logic isn't quite right. You just need to call find() on your original div variable, then set the text(). Try this:

var $div = $('.text');
var $span = $div.find('span.s');
$span.text('some other text');

Or more simply:

$('.text').find('span.s').text('some other text');
Sign up to request clarification or add additional context in comments.

Comments

0

try this for simple text

$('.s').text('some other text');

Or this for HTML text

$('.s').HTML('some <strong>other</strong> text');

Comments

0

You can also execute your same task in one line of Jquery Code

$('div.text span.s:eq(0)).text('whatever the text you want');

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.