JavaScript after() is wrapping quotes around injected HTML

Here is the Javascript:

var bubbles = $('.history .bubble');
bubbles[bubbles.length-1].after('<div class="bubble me">' + text + '</div>');

Here is the HTML:

enter image description here

Any idea why anything I insert is being wrapped in double quotes?

Answer:1

This syntax: bubbles[bubbles.length-1]

… gets you the raw DOM node and not the jQuery object.

This means you are calling the DOM after method and not the jQuery after method.

They behave differently!

If you pass a string to the jQuery method, it will try to parse it as HTML and then insert the resulting DOM nodes.

If you pass a string to the DOM method, it will covert it to a text node and insert that.

You want to call the jQuery method and not the DOM method, so use last() to get a jQuery object wrapping the last DOM node instead of the bracket notation you are using to get the raw DOM node:

bubbles.last().after('<div class="bubble me">' + text + '</div>');
Answer:2

Instead of bracket notation use something like .last() or .eq()

Demo

var text = "X";
var bubbles = $('.history .bubble');
var qty = bubbles.length - 1;
bubbles.eq(qty).after(`<div class="bubble me">${text}</div>`);
bubbles.last().after(`<div class="bubble me">${text}</div>`);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class='history'>
  <div class='bubble'>X</div>
  <div class="bubble">X</div>
</div>
Answer:3

I'm working on a website about donation using Gatsby v2 (Reactjs) and I need an example like open modal window on button donation click using Gatsby or ReactJS. I search on internet and I got nothing. ...

I'm working on a website about donation using Gatsby v2 (Reactjs) and I need an example like open modal window on button donation click using Gatsby or ReactJS. I search on internet and I got nothing. ...

i want to create a webpage like http://seller.flipkart.com . I want to scroll my page first from up to down and then left to right using scrolling button only and background drawing. Can you tell me ...

i want to create a webpage like http://seller.flipkart.com . I want to scroll my page first from up to down and then left to right using scrolling button only and background drawing. Can you tell me ...

I am trying to get parameter value from the url, add it inside a div and then clone that value inside the img src attribute. I can get the parameter value inside a div, and get it to clone as well ...

I am trying to get parameter value from the url, add it inside a div and then clone that value inside the img src attribute. I can get the parameter value inside a div, and get it to clone as well ...

I'm using jasmine-node to run tests for my node.js application. The problem is that if I run the tests one by one with specifying the spec file (jasmine-node spec/file.spec.js) - then everything looks ...

I'm using jasmine-node to run tests for my node.js application. The problem is that if I run the tests one by one with specifying the spec file (jasmine-node spec/file.spec.js) - then everything looks ...