php - Replacing entire tag with shortcode - JavaScript

admin2025-06-02  4

This question already has an answer here: How execute shortcode with javascript (1 answer) Closed 6 years ago.

I'm trying to replace a whole tag with a shortcode, if the tag text equals a value. I'm placing this script into the footer of the page. Here's what I'm trying:

var stockcounter = document.getElementsByClassName("stock in-stock");

if(stockcounter[0].innerHTML=='0 in stock') {
    //Displays timer
    stockcounter[0].outerHTML = do_shortcode('[fergcorp_cdt_single date="Feb 25 2019 18:45:00"]');
}

This doesn't change the target tag at all. Does anyone know what I'm doing wrong?

This question already has an answer here: How execute shortcode with javascript (1 answer) Closed 6 years ago.

I'm trying to replace a whole tag with a shortcode, if the tag text equals a value. I'm placing this script into the footer of the page. Here's what I'm trying:

var stockcounter = document.getElementsByClassName("stock in-stock");

if(stockcounter[0].innerHTML=='0 in stock') {
    //Displays timer
    stockcounter[0].outerHTML = do_shortcode('[fergcorp_cdt_single date="Feb 25 2019 18:45:00"]');
}

This doesn't change the target tag at all. Does anyone know what I'm doing wrong?

Share Improve this question asked Feb 25, 2019 at 16:14 eddiewastakeneddiewastaken 1111 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 0

This looks like javascript. do_shortcode is a php function. I'm not sure the full context, but at the very least, you need to wrap your php function in php tags like this and add an echo:

<?php // Start of php file ?>
<script>
    var stockcounter = document.getElementsByClassName("stock in-stock");

    if(stockcounter[0].innerHTML=='0 in stock') {
        //Displays timer
        stockcounter[0].outerHTML = "<?php echo do_shortcode('[fergcorp_cdt_single date="Feb 25 2019 18:45:00"]'); ?>";
    }
</script>

I'm assuming this script is within a php file.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748868284a314389.html

最新回复(0)