0
I’m learning to code in Javascript and jQuery, and I’m having trouble generating HTML code. Given an HTML body how do I insert HTML code within 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 insert for example before the <div>
or within the <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>
Exit:
Teste1
Teste2
Teste3
Teste4
Ola
I did not know this of id, the intention is to make it as simple as possible, in the event of the above answer I would put id in the body
– Vinicius Morais
@Viniciusmorais the ideal is to add the ID’s where you want to manipulate. This is easier. When you have a large project, you may end up "getting lost" with the amount of elements. That’s why you can add a
id="myId"
orclass="myClass"
– Valdeir Psr