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>
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>
Share
Improve this question
asked Jan 29, 2012 at 19:15
UserIsCorruptUserIsCorrupt
5,02515 gold badges40 silver badges42 bronze badges
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... – Matías Fidemraizer Commented Jan 29, 2012 at 19:21
- do you know what do you want?? these answers are fine to your question. – Ali Youhanaei Commented Jan 29, 2012 at 19:30
6 Answers
Reset to default 9Not 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);
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);
});
Just like this:
$('button').click(function() {
$('div').append(example + "blah");
});
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)
You will have to name your div like this:
<div id="one"> </div>
and put the jQuery code like this
$('#one').html(example);
Maybe I misunderstood your question, but is this a simple string concatenation?
var example = "Test";
$('button').click(function() {
example += "blah"; // ????
$('div').append(example);
});