Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 320
UserIsCorrupt Avatar asked Oct 28 '25 22:10

UserIsCorrupt


2 Answers

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);
like image 72
Selvakumar Arumugam Avatar answered Oct 30 '25 13:10

Selvakumar Arumugam


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);
});
like image 34
Sahar Farsijani Avatar answered Oct 30 '25 14:10

Sahar Farsijani