javascript - Generate innerHTML at the start of an element - Stack Overflow

I want to generate HTML-code at the beginning of an existing element.This is the existing HTML-element

I want to generate HTML-code at the beginning of an existing element.

This is the existing HTML-element:

<div id="container">
  <p>end of the element</p>
</div>

With my current solution I add the generated element beneath the existing content:

document.getElementById(container).innerHTML += "<p>start of the element</p>";

How to add content above the existing content of an element with innerHTML?

I want to generate HTML-code at the beginning of an existing element.

This is the existing HTML-element:

<div id="container">
  <p>end of the element</p>
</div>

With my current solution I add the generated element beneath the existing content:

document.getElementById(container).innerHTML += "<p>start of the element</p>";

How to add content above the existing content of an element with innerHTML?

Share Improve this question edited Jul 9, 2023 at 12:43 Penny Liu 17.6k5 gold badges86 silver badges108 bronze badges asked Jun 11, 2018 at 9:14 iamrobin.iamrobin. 1,6343 gold badges20 silver badges31 bronze badges 1
  • document.getElementById(container).innerHTML = "<p>start of the element</p>" + document.getElementById(container).innerHTML; – Nikhil Aggarwal Commented Jun 11, 2018 at 9:16
Add a ment  | 

4 Answers 4

Reset to default 6

Prepend document.getElementById('container').innerHTML to the assignment and remove += shorthand:

document.getElementById('container').innerHTML = "<p>start of the element</p>" + document.getElementById('container').innerHTML;
<div id="container">
  <p>end of the element</p>
</div>

There is a relatively new function especially for that: insertAdjacentHTML. Which has four options: before the opening tag (beforeBegin), right after the opening tag (afterBegin), just before the closing tag (beforeEnd), and after the closing tag (afterEnd). In your case:

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>Demo</title>
</head>
<body>
    <div>
        <p>First paragraph in the code, but should bee second after the Javascript fired.</p>
    </div>
    <script>
        document.querySelector('div').insertAdjacentHTML('afterBegin','<p>Second paragraph in the code, but should bee first.</p>')
    </script>
</body>
</html>

This code might work

var newItem = document.createElement("p");
newItem.innerHTML = "start of the element";
var container = document.getElementById("container");
var currentItem = document.getElementById("container").firstChild;
container.insertBefore(newItem, currentItem);

With jQuery:

<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<script>
$('#container').prepend("<p>start of element</p>");
</script>

$('#addText').click(function() {
  $('#container').prepend("<p>Prepended Text:" +
    $('#textToPrepend').val() +
    "</p>");
});
#container {
  border: 2px solid black;
  padding: 10px;
  margin: 10px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="textToPrepend" /><br/>
<button id="addText">Add text to div</button>
<div id="container">
  <p> Div Container
    <p/>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744188182a4562306.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信