'How to add shortcode in wordpress using Javascript?

I want to put a shortcode in a WordPress website using JavaScript.

Website structure is

<div class="shop-container">
    <div class="wrap">[shortcode here]</div>
    <div class="wc-pr"> </div>
</div>

Firstly, i put div element to above wc-pr using below code.

<script>
    const parent5 = document.querySelector('.shop-container');
    const billingField1 = document.querySelector('.wc-pr');

    const newDiv = document.createElement('div');
    newDiv.setAttribute('id', 'wrap');

    parent5.insertBefore(newDiv, billingField1);
</script>

Then i tried below code.

newDiv.innerHTML = `<?php do_shortcode("[shortcode here]");?>`;
newDiv.innerText = `<?php do_shortcode("[shortcode here]");?>`;

But not working. innertext is diplayed as raw, Hhtml is not showing.

Please let me know how do i solve this

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source