Mike
Mike

Reputation: 980

how do i change contents of a div within a div?

What I am trying to do is change the contents of a div within a div. I can't seem to access it.

dialog is the parent while dialogChange is the child.

When I do:

$("#dialog").text("New Text");

It'll replace everything within the parent, dialog.

But when I do:

$("#dialogChange").text("New Text");

Nothing changes.

So how can I access the child within a parent?

Upvotes: 0

Views: 113

Answers (4)

Luke101
Luke101

Reputation: 65238

How about something like this:

$('parent').find('child').text('New Text');

Upvotes: 1

Zachary Wright
Zachary Wright

Reputation: 24070

Instead of using the text method, which replaces the entire contents of the div, you should try some of these methods:

.before()
.after()
.append()

For example, you could use $("#dialogChange").before("New Text") to insert something before that div.

Play around for the exact effect you want, and use the API as a guide:

http://api.jquery.com/

Upvotes: 1

Brandon Montgomery
Brandon Montgomery

Reputation: 6986

If you do $("#dialog").text("New Text");, this will effectively remove dialogChange, so $("#dialogChange").text("New Text"); won't work.

Upvotes: 5

Oliver Moran
Oliver Moran

Reputation: 5157

Your jQuery looks good. Possibly the problem is in your HTML.

Or by any chance have your replaced the contents of the parent (and therefore deleted the child) before you call the code affecting the child?

Upvotes: 0

Related Questions