1

I am using WordPress and I have replace some content inside a div using Javascript. It is working.

<script type="text/javascript">
var yourHTML = '<div class="mydiv">Test</div>';
document.getElementsByClassName('services-right')[0].innerHTML = yourHTML;
</script>

Now I want to insert a shortcode [jw_easy_logo slider_name="partners"] but it is not working when I am inserting then nothing happen on front-end.

<script type="text/javascript">
var yourHTML = '<div class="mydiv">[jw_easy_logo slider_name="partners"]</div>';
document.getElementsByClassName('services-right')[0].innerHTML = yourHTML;
</script>

Shortcode is not working but normal HTML is working.

1
  • instead of sort code you need to update HTML and for it you have to apply ajax code for it and replace entire HTML of response of ajax via java script. Commented Jan 2, 2017 at 12:58

2 Answers 2

2

Try this code

First get the output from shortcode using the do_shortcode().

<script type="text/javascript">
    var yourHTML = '<div class="mydiv"><?php do_shortcode("[jw_easy_logo slider_name=partners]");?></div>';
    document.getElementsByClassName('services-right')[0].innerHTML = yourHTML;
</script>

NOTE: this code is working if shortcode is used the return not echo

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

1 Comment

It is a good idea but I am using a plugin "Header and Footer Scripts" which allow to insert only JavaScript. It will be very suitable if it start work only by JavaScript.
1

in addition to what @Ankur said: "NOTE: this code is working if shortcode is used the return not echo"

even if the shortcode echos, you can use ob_start() and ob_get_clean() around the shortcode, so it won't print it striaght away.

Comments

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.