# Extracting variable from HTML blockl

**URL:** <https://forum.bubble.io/t/extracting-variable-from-html-blockl/210215>\
**Category:** Need help\
**Created:** [June 7, 2022, 10:36pm UTC](https://forum.bubble.io/t/extracting-variable-from-html-blockl/210215 "2022-06-07T22:36:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sondreberg91](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@sondreberg91](https://forum.bubble.io/u/sondreberg91)\
**Post date:** [June 7, 2022, 10:36pm UTC](https://forum.bubble.io/t/extracting-variable-from-html-blockl/210215/1 "2022-06-07T22:36:38Z")

</div>

hey guys,

I have been ripping my hair off trying to figure this out and I have been searching through the forum and tried all aproches.

In all its simplicity I want to get a variable from a javascript inside a HTML block and use it for other blocks in bubble. first goal is just getting it top appear in a text block. The script is basically using model-viewer to display a 3d model and when you click on the muscles on the encorche, they change color and their names appear.

I am aware of the toolbox javascript to bubble plug in and I have been able to get this to work in a test script. In the script I want to extract the variable from however, it doesnt work. This is probably because the variable changes on a click event (which I also want the extracted bubble element to do).

Here is the script I am talking about:

NOTE: The script itself already does what I want - display the name of the muscle you click on 3d model. But I want to use this muscle name inside bubble. Perhaps its easier to get the text I want to import to bubble directly from the

? I tried this too, but I couldnt find a way to get the value of the id into a variable that could be used with javascript to bubble plug in.

PS: sorry for messy code, I am learning by doing 🙂

```
    <!-- Import the component -->
    <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>

 <model-viewer  
      style="margin: auto; min-width: calc(100vh - 50vh); min-height: calc(100vh); top: 0px; position: sticky;" bounds="tight" 
      id="pickMaterial" loading="eager" 
      shadow-intensity="0.5" exposure="1.5" 
      camera-controls src="https://s3.amazonaws.com/appforest_uf/f1654607332726x973226565179949200/ecorche_MQ-Draco.glb" 
      environment-image="neutral" 
      ar ar-modes="webxr scene-viewer quick-look" 
      alt="A Material Picking Example">
    </model-viewer>

    <div type="text" id="data"></div>

    <script type="module">

    var previousMaterial;
    var muscles = ['this has a "search for" in the database'];
    var name;

    // INITIALISE MUSCLE GROUP COLORS
    const modelViewerParameters = document.querySelector("model-viewer");
    var i = 0;
    modelViewerParameters.addEventListener("load", (ev) => {
        
    for (let [key, value] of Object.entries(modelViewerParameters.model.materials)) {
        i = i+1;
      // Defaults to gold
      let materials = modelViewerParameters.model.materials[i];
      
       var names = materials.name;

      console.log(names);
      
      if(muscles.includes(names)) {
      materials.pbrMetallicRoughness.setBaseColorFactor([0.7294, 0.5333, 0.0392]);
      }
    }
    })

    // COLOR CLICKED MUSCLES

      const modelViewer = document.querySelector("model-viewer#pickMaterial");
      modelViewer.addEventListener("load", () => {
      const changeColor = async (event) => {
      
      let material = modelViewer.materialFromPoint(event.clientX, event.clientY);
      
        if(material != null) {
          document.getElementById("data").innerHTML=material.name.replace('_', ' ').replace('_Muscle', '').replace('Muscle', ' ');
          // extract name of clicked muscle group
          name = document.getElementById("data").innerHTML;
          console.log(name)
          
    material.pbrMetallicRoughness.setMetallicFactor(0)
          //[0,1,0,1]
          // random color: [Math.random(), Math.random(), Math.random()]
          if(previousMaterial != null){
             previousMaterial.pbrMetallicRoughness.setMetallicFactor(1);
          }
          previousMaterial = material;
        }
      }

      modelViewer.addEventListener("click", changeColor);

    });

    </script>
```

---

<div class="post-metadata">

**Author:** ![cmarchan](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/cmarchan/32/253062_2.png) [@cmarchan](https://forum.bubble.io/u/cmarchan)\
**Post date:** [June 8, 2022, 3:08am UTC](https://forum.bubble.io/t/extracting-variable-from-html-blockl/210215/2 "2022-06-08T03:08:17Z")

</div>

Hello @sondreberg91

[![](https://img.youtube.com/vi/9jVEzPLRebI/maxresdefault.jpg "The Bubble.io Toolbox Plugin | How to save a value from Javascript to Bubble | Bubble.io Tutorials") ](https://www.youtube.com/watch?v=9jVEzPLRebI)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/bubble/original/3X/2/8/2847faa74692c2ffa5d939d3dc3724cdd060d247.png) [@system](https://forum.bubble.io/u/system)\
**Post date:** [August 16, 2022, 10:37pm UTC](https://forum.bubble.io/t/extracting-variable-from-html-blockl/210215/3 "2022-08-16T22:37:37Z")

</div>

Closed on August 16, 2022, 10:37pm UTC

This topic was automatically closed after 70 days. New replies are no longer allowed.
