jonmrich
jonmrich

Reputation: 4323

Get text from div, add some more text, save as variable

Here's what I've got:

<div id="fivearea">Something</div>

Then later:

var 5popup= $('#fivearea').text();

This makes 5popup = "Something".

However, I want to add some more text to what's in the div and save that as a variable. Psuedo-code:

var 5popup= $('#fivearea').text()+"some additional text";

What's the correct way to do this?

Upvotes: 2

Views: 2255

Answers (3)

Ben Pearce
Ben Pearce

Reputation: 7094

Replacing the text inside the node below:

<div id="testId">hello</div>

Can be accomplished as follows

$("#testId").html("good bye");

Resulting in the html:

<div id="testId">good bye</div>

Similarly the text inside the node can be saved to a variable as follows:

var myTest = $("#testId").html();

If you want to add to the text inside a node without deleting anything, this can be accomplished with the append function. Given the following html.

<div id="testId">hello</div>

The code:

$("#testId").append(" world");

Will result in the html:

<div id="testId">hello world</div>

Upvotes: 1

user3434588
user3434588

Reputation:

I think this is what you want:

var text = $('#fivearea').text();
text += "Your text here";
$('#fivearea').text(text);

If you want to access the text, use the variable text. Hope this helped.

Upvotes: 0

Full Stack Alien
Full Stack Alien

Reputation: 12501

You will want to add to the current value of the variable like this:

    5popup += "some additional text";

Upvotes: 2

Related Questions