7stud
What do I have to do to use JQuery in a Phoenix app? Suppose I create my Phoenix app like this:
/phoenix_apps% mix new --umbrella a_umbrella
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating apps
* creating config
* creating config/config.exs
Your umbrella project was created successfully.
Inside your project, you will find an apps/ directory
where you can create and host many apps:
cd a_umbrella
cd apps
mix new my_app
Commands like "mix compile" and "mix test" when executed
in the umbrella project root will automatically run
for each application in the apps/ directory.
~/phoenix_apps% cd a_umbrella/apps
~/phoenix_apps/a_umbrella/apps% mix new my_app --sup
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/my_app.ex
* creating lib/my_app/application.ex
* creating test
* creating test/test_helper.exs
* creating test/my_app_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd my_app
mix test
Run "mix help" for more commands.
That gives me this directory structure:
~/phoenix_apps/a_umbrella% tree .
~/phoenix_apps/a_umbrella% tree .
.
├── README.md
├── apps
│ └── my_app
│ ├── README.md
│ ├── lib
│ │ ├── my_app
│ │ │ └── application.ex
│ │ └── my_app.ex
│ ├── mix.exs
│ └── test
│ ├── my_app_test.exs
│ └── test_helper.exs
├── config
│ └── config.exs
└── mix.exs
6 directories, 9 files
What do I need to do to use JQuery in a template?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 19 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
7stud
I couldn’t get jQuery to work. Thanks for your help.
03juan
That’s a very good question. This style is a side effect only type of import that should run the script’s global code.
Maybe try to compile it separately in the esbuild args string and import it as a separate asset in the HTML head, and skip the extra step through
app.js?Or just straight-up inline the
jquery_setup.jscode inapp.js?7stud
Hurray! Clap, clap! I got all the methods you suggested to work. Somebody understands scope in javascript!
Out of curiosity, why didn’t my attempts with JQuery work? I clicked the link:
Download the uncompressed, development jQuery 3.6.4and I copied the code and put it in
..assets/vendor/jquery.js.In
../assets/js/jquery_setup.js, I have:And, in app.js I have:
import "./jquery_setup.js"But, in my browser I get an error in the js console that says
$isn’t recognized. I looked through the jQuery code, and I don’t think it exports “jQuery” or “$”. If it would take too much time to figure out, don’t bother.03juan
This will import
hellointo the scope ofapp.js, but will not make it available to the HTML document automatically unless you assign the function to the globalwindowelement.A good next step would be to namespace your exported functions, then assign that to the global element:
This can start to get unwieldy when you have a lot of functionality in multiple HTML scripts.
I would instead move them to
app.jsand set up your code from there:Lastly, if
app.jsalso gets too large for your liking, or you only want to have certain features running in specific pages, you can move the relevant code to its own module, haveesbuildcompile it separately, then load it into your page.add
js/my_feature.jsto the esbuild args string (and restart the server to apply changes to config files)Exadra37
I also highly recommend Elixir in action, but PragDave is the only one that spends the time to make your mind shift from OOP to FP and thats why I always recommend his book first.
Another excellent book is Real-Time Phoenix by @sb8244:
Real-Time Phoenix
Build and deploy real-time, scalable applications using Phoenix Channels to optimize performance and minimize resource use.
7stud
I’m using Phoenix 1.7.2, which I installed a few days ago with the command:
mix.exs:
I’ll try adding my js file to a non umbrella project and see if I get different results.
codeanpeace
Does your umbrella project happen to be created using an older version of the Phoenix generators?
Cannot load assets in Phoenix umbrella app
7stud
I purchased “Elixir in Action” using a discount code on this forum. I’ve read part of it. It is very good. At the same time, I purchased “Phoenix in Action”, and I’m revisiting it now.
7stud
I spent a few years learning erlang before elixir.
I’ve already read it.
7stud
I tried creating a js module, but it doesn’t work for me. Here’s what I tried:
../assets/vendor/my_functions.js
../assets/js/app.js
Without the braces around
hello, I got the following error:Template:
Error in the javascript console in my browser:
which points here: