> For the complete documentation index, see [llms.txt](https://dev.ubidots.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev.ubidots.com/dashboards-and-widgets/html-canvas/code-editor/javascript-tab.md). Agents can also search and read this documentation through its [MCP server](https://dev.ubidots.com/~gitbook/mcp).

# JavaScript tab

<figure><img src="https://884329393-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MhzNRg0B4ECiNXc093G%2Fuploads%2FkbXmMlOnz063JXUzLI6B%2Fimage.png?alt=media&amp;token=7ebfb887-c470-4073-833c-8ca9cb4d80f7" alt="" width="563"><figcaption></figcaption></figure>

JavaScript is a scripting language that lets users interact with web page elements, define page logic, and dynamically update content.

### Examples

<table><thead><tr><th width="546">JavaScript code</th><th>Action</th></tr></thead><tbody><tr><td><code>const Paragraph = document.querySelector("p");</code></td><td>Selects a paragraph element and stores a reference to it in the <em>Paragraph</em> variable</td></tr><tr><td><code>function updateName()</code><br><code>{</code><br><code>const name = prompt("Enter a new name");</code><br><code>para.textContent = Player 1:</code><br><code>${name};</code><br><code>}</code></td><td>Defines a function that can be called with <em>updateName()</em></td></tr><tr><td><code>Paragraph.addEventListener("click", updateName);</code></td><td>Attaches a click event listener to the Paragraph element so the <em>updateName</em> function runs when the paragraph is clicked</td></tr><tr><td><code>const para = document.getElementById("player");</code></td><td>Gets a reference to the element with the ID "player" from the DOM</td></tr></tbody></table>

### Advanced example #1

Assume the following requirement:

* Create a paragraph element that displays the message "PLAYER 1: name", where *name* is a value set by the user when the paragraph element is clicked.

This can be accomplished with the following HTML + CSS + JavaScript example:

```html
<!DOCTYPE html>
<html>
<head>
  <style>
    p {
      /* set the font family, size, and style */
      font-family: "Helvetica Neue", Helvetica, sans-serif;
      letter-spacing: 1px;
      text-transform: uppercase;
      /* set the border, background, color, and box shadow */
      border: 2px solid rgba(0, 0, 200, 0.6);
      background: rgba(0, 0, 200, 0.6);
      color: rgba(255, 255, 255, 1);
      box-shadow: 1px 1px 2px rgba(0, 0, 200, 0.4);
      /* set the border radius and padding */
      border-radius: 10px;
      padding: 3px 10px;
      /* set the display to inline-block and cursor to pointer */
      display: inline-block;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <!-- add an ID attribute to the <p> element -->
  <p id="player-name">Player 1: Chris</p>
  <script>
    // get a reference to the <p> element using its ID
    const para = document.getElementById("player-name");

    // add a click event listener to the <p> element
    para.addEventListener("click", updateName);

    function updateName() {
      // prompt the user to enter a new name
      const name = prompt("Enter a new name");

      // update the text content of the <p> element
      para.textContent = `Player 1: ${name}`;
    };
  </script>
</body>
</html>
```

<figure><img src="https://884329393-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MhzNRg0B4ECiNXc093G%2Fuploads%2FTo2DExnJEnlUDYspYjZp%2F1.gif?alt=media&amp;token=d0717c85-b6ab-4812-aff9-570c3daadb41" alt=""><figcaption></figcaption></figure>

The same result can be reproduced in Ubidots by using the HTML Canvas widget as follows:

* Add the following HTML to the widget's HTML tab:

```html
<!-- add an ID attribute to the <p> element -->
<p id="player-name">Player 1: Chris</p>
```

* Add the following styles to the widget's CSS tab:

```css
p {
      /* set the font family, size, and style */
      font-family: "Helvetica Neue", Helvetica, sans-serif;
      letter-spacing: 1px;
      text-transform: uppercase;
      /* set the border, background, color, and box shadow */
      border: 2px solid rgba(0, 0, 200, 0.6);
      background: rgba(0, 0, 200, 0.6);
      color: rgba(255, 255, 255, 1);
      box-shadow: 1px 1px 2px rgba(0, 0, 200, 0.4);
      /* set the border radius and padding */
      border-radius: 10px;
      padding: 3px 10px;
      /* set the display to inline-block and cursor to pointer */
      display: inline-block;
      cursor: pointer;
}
```

* Add the following code to the widget's JavaScript tab:

```javascript
// get a reference to the <p> element using its ID
const para = document.getElementById("player-name");

// add a click event listener to the <p> element
para.addEventListener("click", updateName);

function updateName() 
{
    // prompt the user to enter a new name
    const name = prompt("Enter a new name");

    // update the text content of the <p> element
    para.textContent = `Player 1: ${name}`;
};
```

This is the result on a Ubidots dashboard:

<figure><img src="https://884329393-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MhzNRg0B4ECiNXc093G%2Fuploads%2FbNTM27cxKKxhvO3AdatE%2F1.gif?alt=media&amp;token=6c374255-32b4-4f19-9c60-2b0e19987414" alt="" width="563"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dev.ubidots.com/dashboards-and-widgets/html-canvas/code-editor/javascript-tab.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
