javascript – can you merge a div tag-ThrowExceptions

Exception or error:

I am making an image editing program in HTML/CSS/JS(with jquery), and need to merge/unmerge a div tag.
I’ve tried deleting the dive tag and creating a new one but I could not figure it out.

<div id='one'></div>
<div id='two'></div>
var 1 =$('#one').html;
var 2 = $('#two').html;
$('#1').html(1 + 2);
How to solve:

There are several issues with your code:

  • JavaScript variables cannot start with a number (0-9).
  • There isn’t a div with an id of 1.
  • $().html is a function and must be called as $().html() with parentheses.

By fixing those issues and adding another line of JavaScript, you’ll get a complete webpage that merges the HTML content of both divs into the first one, then removes the second div:

<!DOCTYPE html>
<div id='one'>One</div>
<div id='two'>Two</div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
var one = $('#one').html();
var two = $('#two').html();
$('#one').html(one + two);
$('#two').remove();
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *