2

I have a HTML structure like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<table id ="x">
  <tr>
    <td class='col1'>
      Hello <span class='name'></span> How are u ?
    <td>
  </tr>
</table>

And here is my jQuery code:

var variable = $('table#x').find(".col1").html().find("span.name").append(' Jack..!').replace('u', 'you');
alert(variable);

Now, I want this output:

<td class='col1'>Hello <span class='name'>Jack..!</span> How are you ?<td>

How can I do that?

1

2 Answers 2

4

You already have a class you can target, 'name'. You can simply add the text to this:

$('.name').text('Jack..!')

https://jsfiddle.net/2j7rxwyj/

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

Comments

2

Based on my comment, you could use the following:

Updated Example

$('table#x .col1 span.name').append(' Jack..!').parent().text(function () {
  return $(this).text().replace('u', 'you');
});

There were a couple issues in your code. For one, you can't chain .append() after the .html() method. In addition, the .replace() method wasn't changing the text. To fix this, you could pass an anonymous function to the .text() method and return the replaced text.

1 Comment

Sorry for asking again, but how can I do that as sibling instead of into-element (append())? I mean is something like this ... Hello <span class='name'></span>Jack..! How ...

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.