Nicolas Forney Nicolas Forney -4 years ago 142
HTML Question

Avoid line break when using jquery append() or html()

At first I have this in my DOM :

<li>
<span>A</span>
</li>


When i do this :

$("li").append("<span>B</span>");




Here is what I got if I look into the DOM :

<li>
<span>A</span>
<span>B</span>
</li>




But I would like to have

<li>
<span>A</span><span>B</span>
</li>




I need to display span on the same line in order to avoid displaying a white space between the two elements.

With javascript innerHTML the problem is the same as jQuery use it for append and html function.

Here is a jsFiddle

Thank you for your help

Answer Source

You can solve, or at least avoid, the problem by simply using after() in place of append():

$("li span").last().after("<span>C</span>");

$("li span").last().after("<span>C</span>");
$('#generatedHTML').text($('li').html());
#generatedHTML {
  white-space: pre-wrap;
  font-family: mono;
  padding: 0.5em;
  margin: 0;
  background-color: #eee;
}
#generatedHTML::before {
  content: "Created HTML: ";
  color: #999;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <span>A</span>
  </li>
</ul>
<div id="generatedHTML"></div>

Also, please note that I corrected your HTML: an li is only valid when a direct-child of a ul or ol (it should not be contained within any other element).

Also, you could use a somewhat more-complicated alternative:

$("li").append("<span>B</span>").contents().filter(function(){
    return this.nodeType === 3;
}).remove();

$("li").append("<span>B</span>").contents().filter(function(){
    return this.nodeType === 3;
}).remove();
$('#generatedHTML').text($('li').html());
#generatedHTML {
  white-space: pre-wrap;
  font-family: mono;
  padding: 0.5em;
  margin: 0;
  background-color: #eee;
}
#generatedHTML::before {
  content: "Created HTML: ";
  color: #999;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <span>A</span>
  </li>
</ul>
<div id="generatedHTML"></div>

While it's been some time, I thought it was worth also adding a plain JavaScript version of the above:

let htmlToAppend = '<span>B</span>',
  span = document.querySelector('li span');

span.insertAdjacentHTML(
  'afterend', htmlToAppend
);
document.querySelector('#generatedHTML').textContent = span.parentNode.innerHTML;

let htmlToAppend = '<span>B</span>',
  span = document.querySelector('li span');

span.insertAdjacentHTML(
  'afterend', htmlToAppend
);
document.querySelector('#generatedHTML').textContent = span.parentNode.innerHTML;
#generatedHTML {
  white-space: pre-wrap;
  font-family: mono;
  padding: 0.5em;
  margin: 0;
  background-color: #eee;
}

#generatedHTML::before {
  content: "Created HTML: ";
  color: #999;
  display: block;
}
<ul>
  <li>
    <span>A</span>
  </li>
</ul>
<div id="generatedHTML"></div>

References:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download