Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript – wrap an object in an html container before appending

I am using a popup lightbox plugin, the plugin generates next/prev buttons but it doesn’t put them in a container. I would like to wrap them in a containing element and have found the spot in the plugin where they are generated but I can’t figure out how to wrap them since they are generated as an object.

This is what the default code for button generation looks like:

mfp.container.append(arrowLeft.add(arrowRight));

Before adding them to the container I would like to wrap them in a div with the class mfp-nav. I tried the following, but it’s not working since the buttons are an object.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

mfp.container.append('<div class="mfp-nav">' + arrowLeft.add(arrowRight) + '</div>');

On the front-end this ends up looking like this:

<div class="mfp-nav">[object Object]</div>

Is there an easy way to wrap this object in a container without looping over the object?

>Solution :

arrowLeft and arrowRight are not HTML strings, they’re DOM elements, so you can’t just concatenate them with other HTML. Create the DOM elements.

let div = document.createElement('div');
div.classList.add('mfp-nav');
div.append(arrowLeft.add(arrowRight));
mfp.container.append(div);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading