Dynamic data javascript

Does this not work anymore? Using Node.js

    <dmx-value id="var1" dmx-bind:value="123"></dmx-value>

    <script>
        function myFunction() {
        var myVar = dmx.parse('var1.value');
        console.log(myVar);
    }
    </script>

I get undefined.

Your post is quite unclear.
Please explain your issue/question more detailed so we can answer it.

Sorry, the code didn’t show up. Fixed it.

Essentially I’m trying to get dynamic data in my javascript so I can add dynamic schema data to my pages. I’m starting basic, but can’t seem to even get the basics to work.

When do you call myFunction()? In the code you pasted, it is just defined and i don’t see it getting called anywhere.

Yes, when I call it in the console it returns undefined.

Ok, but where is var1 being placed? Is it in a content page?

This is just a simplified example based on this:

But yes, It is within the body of the content page.

Well, when it’s on a content page then you need to prefix var1 with content. in your custom js code:

<dmx-value id="var1" dmx-bind:value="123"></dmx-value>

<script>
    function myFunction() {
        var myVar = dmx.parse('content.var1.value');
        console.log(myVar);
    }
</script>

Ok great. I didn’t realize that. What I’m really trying to do is this:

<script>
    function setSchema() {
  var schemaData = {
    "@context": "http://schema.org",
    "@type": "Physician",
    "name": dmx.parse('serverconnect1.data.query.name'),
    "jobTitle": "Aviation Medical Examiner",
    "url": "https://www.ameforesight.com/ame-profile/" + dmx.parse('serverconnect1.data.query.slug'),
    "address": dmx.parse('serverconnect1.data.query.address'),
    "telephone": dmx.parse('serverconnect1.data.query.phone_number'),
    "aggregateRating": {
      "@type": "AggregateRating",
      "ratingValue": dmx.parse('serverconnect4.data.query[0].average_rating'),
      "reviewCount": dmx.parse('serverconnect4.data.query[0].number_of_ratings')
    }
  };
  var script = document.createElement('script');
  script.type = 'application/ld+json';
  script.textContent = JSON.stringify(schemaData);
  document.head.appendChild(script);
}
</script>

The function would then be called when serverconnect4 is complete.

All the elements which you want to access in your custom js code and are located inside a content page must be prefixed with content.

Ok, I think you have given me all I need. Really appreciate the prompt response!

1 Like