Adding text after variable in JavaScript code

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>

Answer

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);

Leave a Reply

Your email address will not be published. Required fields are marked *