1

I have something like <p> <b> old</b> </p>

I want to replace current text inside bold tag such that result is <p> <b> new</b> </p> tag when user clicks on the parent .

I am using jquery like $(this > "b").text("new"); and $(this).children("b").text("new"); but it is not working?

Any help is appreciated..

2
  • 1
    $(this).children("b").text("new"); should work fine if you correctly get <p> element as this. Commented Dec 20, 2013 at 9:49
  • if your <p> contain only a b tag you can use children as this : $(this).children().text("new");. are you sure that this mean the p tag ? Commented Dec 20, 2013 at 9:52

3 Answers 3

3
$(this).find("b").html("new");

should do the trick, assuming there is a click handler on the p element.

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

Comments

1

Take a look at this simple jsFiddle

Find the child element by using children() method that is specified to your element - in this case b. For inner HTML content use html() method with the content you require specified.

Docs:

http://api.jquery.com/children/

http://api.jquery.com/html/

Comments

1

You can add an id to the P tag for the select and use something like this:

$("#p").children("b").text("new");

Check this fiddle : http://jsfiddle.net/zrvmk/

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.