I'm learning to program in JavaScript and jQuery, and I'm having trouble generating HTML code. Given an HTML body how do I insert HTML code inside a specific place?
In the case below I already know how to insert a paragraph at the end of <body>
. But I would like to know how to enter for example before <div>
or else within <div>Teste1</div>
.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script>$(document).ready(function(){$(document.body).append($('<p/>').text('Ola'));});</script></head><body><div>Teste1</div><div>Teste2</div><div>Teste3</div><div>Teste4</div></body></html>
Output:
Teste1
Teste2
Teste3
Teste4
Ola