0

I have a document structure looks like this:

<div class="text">
     Send email 
</div>

How can I insert a span tag after the text so that the final document structure looks like:

<div class="text">
     Send email <span class="envelope"></span>
</div>
0

3 Answers 3

3

Just use jQuery#append. I also print the html structure of the div to see what it is like.

const div = $('.text');

div.append('<span class="envelope">Envelop</span>');

console.log(div.html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="text"> Send email </div>

Sign up to request clarification or add additional context in comments.

Comments

2

you need to use append() method of jquery library it will add span after text.

$( "div.text" ).append( '<span class="envelope"></span>' );

Comments

1

If you want to use Vanilla JS, Try this.

 document.querySelector('.text').innerHTML += `<span class="envelope">sss</span>`;
<div class="text">
 TEXT
</div>

1 Comment

innerHTML is not purposed to populate existing elements. Use appendChild or insertAdjacentHTML instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.