4
var example = "Test" ;

$('button').click(function() {
 $('div').append(example);
});

<button>Whatever</button>
<div></div>

How can I add text after the variable example in the jQuery code?

In other words, in the jQuery code how can I add text (in this example: "blah") after the variable so the HTML code will appear like this

<div>Testblah</div>
2
  • 2
    I answered your question, but, anyway, I find SO isn't for that kind of simple things. Learning JavaScript and checking available string operators should be enough to get it... Commented Jan 29, 2012 at 19:21
  • do you know what do you want?? these answers are fine to your question. Commented Jan 29, 2012 at 19:30

6 Answers 6

9

Not sure if this is what you are looking for,

$('div').html(example + "blah");

Note I have used .html instead of .append. You can also use .text if you gonna insert plain text inside the div.

Above is just a plain javascript string concatenation. You should read about String Operators

Also the above doesn't change the value of var example. If you want the value to be changed then assign the result to the example and set the div html.

 example += 'blah';
 $('div').html(example);
Sign up to request clarification or add additional context in comments.

Comments

2

change to this :

var example = "Test" ;
$('button').click(function() {
  example=example+'blah';
 $('div').append(example);
});

or:

var example = "Test" ;
var exp="blah";
$('button').click(function() {
  example=example+exp;
 $('div').append(example);
});

Comments

1

Just like this:

$('button').click(function() {
    $('div').append(example + "blah");
});

Comments

1

Try using concat (Vanilla JS):

var example = "Test"
//to concatenate:
example = example.concat("blah")
document.write(example)
//if you want a space:
example = example.concat(" blah")
document.write(example)

Comments

0

You will have to name your div like this:

<div id="one"> </div>

and put the jQuery code like this

$('#one').html(example);

Comments

0

Maybe I misunderstood your question, but is this a simple string concatenation?

var example = "Test";

$('button').click(function() {
 example += "blah"; // ????
 $('div').append(example);
});

2 Comments

@amnotiam Is this concatenating "blah" to "Test"? I got lost.
Ah you removed your comment... :(

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.