0

I need help printing javascript variable into a JSON object with its value which it has fetched. Please check my code:

 <script type="text/javascript" data-ampdevmode="">
 var postid = document.querySelector('.status-publish').getAttribute('id').replace("post-", "");
 </script>
 <script type="text/javascript" data-ampdevmode="">
 console.log(postid);
 </script>
 
 <amp-state id="wpp-json-amp"> 
 <script type="application/json" id="wpp-json">
 {
 "ID": postid
 }
 </script>
 </amp-state>

Here "postid" will get the id of WordPress post.

Also its not printing the inside content of <script type="application/json" id="wpp-json"> on amp page.. can anyone help me with this?

asked Feb 13, 2024 at 21:23
2
  • 2
    At least in your snippet you are missing an element with the class status-publish? Please provide some more code in case that element does exist in the DOM somewhere (if not, that's certainly at least a part of your problem). Please also be more precise what exactly it is you're trying to do? Currently this looks very unusal with a lot of <script/> tags but practically almost no code in them, so it's very hard to tell what you're trying to do. Commented Feb 13, 2024 at 21:27
  • It will just fetch the postid like a random number. Commented Feb 13, 2024 at 21:34

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.