# Uibuilder to use jquery (example fails?)

**URL:** https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230
**Category:** General
**Tags:** uibuilder
**Created:** [7 March 2023 13:23 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230 "2023-03-07T13:23:01Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [7 March 2023 13:23 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/1 "2023-03-07T13:23:01Z")

</div>

How can jquery be used within NR/uibuilder? This is necessary to migrate an existing project with intensive use of jquery.

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [7 March 2023 16:54 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/2 "2023-03-07T16:54:41Z")

</div>

Yes, there is a **uibuilder example for using jquery** , but unfortunately it runs into an error:  
`Uncaught ReferenceError: $ is not defined at index.js:24:1`

index.js has this:

```auto
#23 // When JQuery is ready, update
#24 $(document).ready(function () {

```

Also note, beside the the template files index.(html|js|css) there is only:  
`<ip>:1880/jp/uibuilder/vendor` with `socket.io`, `build/esm`, and `node_modules`.  
 ![uibuilder_jq_err](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b88b8e177336fda538428d30fa28c7e56427feb9.png)

My installation has a jquery already:  
`.node-red/node_modules/jquery`  
Would it be possible to reference that with uibuilder?

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [7 March 2023 18:09 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/3 "2023-03-07T18:09:59Z")

</div>

Hello .. can you share your `index.html` to see how you loaded the Libraries ?

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [7 March 2023 19:00 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/4 "2023-03-07T19:00:06Z")

</div>

The index.html is 100% of the example

> **[Example: JQuery](https://github.com/TotallyInformation/node-red-contrib-uibuilder/wiki/Example:-JQuery)**
>
> Easily create data-driven web UI's for Node-RED using any (or no) front-end framework. - TotallyInformation/node-red-contrib-uibuilder

with copy/paste from inside the uibuilder files.  
Also note the example is running with replacing one like:

```auto
    <!-- --- Vendor Libraries - Load in the right order --- -->
    <!-- <script src="../uibuilder/vendor/jquery/dist/jquery.min.js"></script> -->

```

```
<script src="./jquery-3.6.0.min.js"></script>

```

and of course the jquery file is installed under src/ (as with the other index.( ..) files.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [7 March 2023 19:04 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/5 "2023-03-07T19:04:38Z")

</div>

do you have the jquery library installed in the **Libraries** section of your uibuilder node ?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b2170a95290d857cda90f6c1373a1f7dab3b962.png)

so it will be served under /vendor

and then load it in code (index.html)

```auto
<!-- --- Vendor Libraries - Load in the right order --- -->
    <script src="../uibuilder/vendor/jquery/dist/jquery.min.js"></script>

```

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [7 March 2023 19:10 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/6 "2023-03-07T19:10:29Z")

</div>

Not loaded yet, forgive me for not noticing that this is an explicit requirement.  
But can you give the required loading statement, please?

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [7 March 2023 19:22 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/7 "2023-03-07T19:22:45Z")

</div>

possibly its not mentioned in this older jquery example in the wiki because  
if i remember correctly (i could be wrong) in previous uibuilder versions some libraries like jquery, vue 2, bootstrap-vue were preloaded in the Libraries section so no additional installation was needed. now you need to install them

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [7 March 2023 19:31 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/8 "2023-03-07T19:31:16Z")

</div>

Ha, well isn't that typical! Yours is the first request for jQuery in ages and I just removed the jQuery example from the examples for uibuilder v6.1 because it was out of date. The WIKI entry undoubtedly also needs updating.

This is a bare-bones example for uibuilder v6.1

!! Oops - too quick for my own good - this is an updated example that actually works properly!

index.html

```auto
<!doctype html>
<html lang="en"><head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="../uibuilder/images/node-blue.ico">

    <title>jQuery Example - Node-RED uibuilder</title>
    <meta name="description" content="Node-RED uibuilder - jQuery Example">

    <!-- Your own CSS (defaults to loading uibuilders css)-->
    <link type="text/css" rel="stylesheet" href="./index.css" media="all">

    <!-- #region Supporting Scripts. These MUST be in the right order. Note no leading / -->
    <script defer src="../uibuilder/vendor/jquery/dist/jquery.min.js"></script>
    <script defer src="../uibuilder/uibuilder.iife.min.js"></script>
    <script defer src="./index.js">/* OPTIONAL: Put your custom code in that */</script>
    <!-- #endregion -->

</head><body class="uib">
    
    <h1 class="with-subtitle">jQuery Example</h1>
    <div role="doc-subtitle">Using the uibuilder IIFE library.</div>

    <div id="more"><!-- '#more' is used as a parent for dynamic HTML content in examples --></div>

</body></html>

```

index.js

```auto
/** The simplest use of uibuilder client library
 * See the docs if the client doesn't start on its own.
 */

$(document).ready(function () {

    // Listen for incoming messages from Node-RED and action
    uibuilder.onChange('msg', (msg) => {
        console.log('msg received', msg)
        // do stuff with the incoming msg
        $('#more').text(msg.payload)
    })
    
})

```

And to round it off, an example flow:

```auto
[{"id":"ef2c6bdb83f45e40","type":"inject","z":"ff9704678e3a4b61","name":"Msg","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"msg-from-nr","payload":"A message from Node-RED","payloadType":"str","x":230,"y":760,"wires":[["0e38f1543880aa12"]]},{"id":"f8176cc4d8409322","type":"debug","z":"ff9704678e3a4b61","name":"std msg output","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"","statusType":"counter","x":735,"y":740,"wires":[],"l":false},{"id":"174c8a1a9edc3178","type":"debug","z":"ff9704678e3a4b61","name":"ctrl msg output","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"","statusType":"counter","x":735,"y":800,"wires":[],"l":false},{"id":"0e38f1543880aa12","type":"uibuilder","z":"ff9704678e3a4b61","name":"","topic":"","url":"uib-jquery-example","fwdInMessages":false,"allowScripts":false,"allowStyles":false,"copyIndex":true,"templateFolder":"blank","extTemplate":"","showfolder":false,"reload":true,"sourceFolder":"src","deployedVersion":"6.2.0-dev","showMsgUib":true,"credentials":{},"x":580,"y":760,"wires":[["f8176cc4d8409322"],["174c8a1a9edc3178"]]},{"id":"29c3f512d2ec644d","type":"inject","z":"ff9704678e3a4b61","name":"Reload","props":[{"p":"_ui","v":"{\"method\":\"reload\"}","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"reload","x":230,"y":880,"wires":[["0e38f1543880aa12"]],"info":"Sends a pre-formatted msg to the front-end that\r\ncauses the page to reload itself."},{"id":"1d65510c9840813a","type":"inject","z":"ff9704678e3a4b61","name":"Toggle Visible Msgs","props":[{"p":"_uib","v":"{\"command\":\"showMsg\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":800,"wires":[["0e38f1543880aa12"]]},{"id":"84eb0817119a73dc","type":"inject","z":"ff9704678e3a4b61","name":"Toggle Visible Status","props":[{"p":"_uib","v":"{\"command\":\"showStatus\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":840,"wires":[["0e38f1543880aa12"]]}]

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [7 March 2023 20:00 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/9 "2023-03-07T20:00:06Z")

</div>

And the WIKI entry now updated to match this example.

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [7 March 2023 21:18 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/10 "2023-03-07T21:18:49Z")

</div>

Thanks for rework.

I'm sure it's described somewhere how to add the jquery as said on the libraries tab.  
Maybe you are kind enough to add it to the example (and/or this chat) ...I am lost with it atm 🤕

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [7 March 2023 21:34 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/11 "2023-03-07T21:34:59Z")

</div>

OK, missed that. It is remarkably simple 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f939fe9f844f8ce37675884960a8cc39328e41db.png)

On the libraries tab, click the small "Add" button and type in "jquery" (without the quotes) into the box

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a0bf4420689c40cb52db582794c6bbdaea194442.png)

Then click the install button. There is a small animation next to the add button when it is installing. The Node-RED log gives a little information. After a few seconds, the animation stops and you are ready to go.

The library manager attempts to give you a clue as to what URL to put in your script link but just bear in mind that it can't always work it out because it relies on library authors to code up their package.json files correctly. For jQuery for example, it lists `jquery.js` which will work but it is better to use `jquery.min.js` to get the smaller production version.

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [7 March 2023 21:46 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/12 "2023-03-07T21:46:09Z")

</div>

👍 😉  
OK, got it!  
Now it's time to see how that works out with migrating my previous project  
_ **Thanks!** _

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [6 May 2023 21:46 UTC](https://discourse.nodered.org/t/uibuilder-to-use-jquery-example-fails/76230/13 "2023-05-06T21:46:32Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
