Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append string in jQuery without quotes?

Let's say we have a paragraph:

<p>txt0</p>

... and we want to append some text:

$("p").append("_txt1");
$("p").append("_txt2");
$("p").append("_txt3");

The result will be, as expected:

txt0_txt1_txt2_txt3

However if we inspect the result in browser, what it really is:

<p>
   "txt0"
   "_txt1"
   "_txt2"
   "_txt3"
</p>

There are 4 different strings that are only rendered as one. The problem is I'm creating a flash object dynamically and appending strings this way will not work because of the quotes. I really need it to be one continuous string like this:

<p>
   txt0_txt1_txt2_txt3
</p>

Is there a way to append in such a way? Or to remove all the quotes afterwards?

PS. before you say to make one big string before appending, that won't work because the string is too big and for ex. it works in Chrome but not in Firefox or IExplorer (but that's a different issue).

like image 299
EricM Avatar asked Mar 20 '26 11:03

EricM


2 Answers

Use text, otherwise you're appending a new TextNode everytime:

var $p = $('p');
$p.text('txt0');
$p.text($p.text() + '_txt1');

Or with textContent it's less confusing:

var p = $('p')[0];
p.textContent += 'txt0';
p.textContent += '_txt1';
...
like image 180
elclanrs Avatar answered Mar 22 '26 01:03

elclanrs


You can manipulate the html inside the p-tag this way:

$('p').html($('p').html() + '_text');
like image 33
DonnyDee Avatar answered Mar 22 '26 00:03

DonnyDee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!