There are many ways to do this. One significant difference in methods is if you choose to create the elements first using Document.createElement()
and then insert the elements, or create and insert the elements in one step using one of the methods that allows you to insert HTML text.
Because it is simpler, but not necessarily better, the examples below show creating and inserting the two <div>
elements in a single step using methods that allow inserting HTML text into the DOM.
JavaScript:
One is to use insertAdjacentHTML()
and specify that it is to be inserted afterend
of the element you are using.
document.querySelector()
is used to find the first <div class="divstudent">
. Then insertAdjacentHTML()
is used to add the additional <div>
elements. Element.removeAttribute()
is then used to remove the class="divstudent"
. Note: if we had just wanted to set teh class
to something different, even ''
, then we could have used Element.className
.
NOTE: In this answer, text identifying each <div>
has been added to the <div>
s so there is something visible in the examples in this answer.
//Find the first <div class="divstudent"> in the document
var studentDiv = document.querySelector('div.divstudent');
//Insert two new <div> elements.
studentDiv.insertAdjacentHTML('afterend','<div>2</div><div>3</div>');
//Remove the class="divstudent"
studentDiv.removeAttribute('class');
<div class="divstudent">1</div>
jQuery:
While your question is tagged jQuery, a comment you posted implies you are just using JavaScript. Thus, I am not sure if jQuery works for you.
If you want to use jQuery, then you can use .after()
to add the <div>
elements. You can then use .removeAttr()
to remove the class="divstudent"
.
// Get the first <div class="divstudent">.
// Store it in a variable so we only walk the DOM once.
var $studentDiv = $('div.divstudent').eq(0);
//Add the two new <div> elements
$studentDiv.after('<div>2</div><div>3</div>');
//Remove the class="divstudent"
$studentDiv.removeAttr('class');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="divstudent">1</div>