rennz555

rennz555

Hello,

I’m working on a practice project of mine in Elixir/Phoenix, but wanted to find out how to disable a button in Elixir?

For example, my application requires two text fields before a user can click “submit”. If one or both text fields are empty, then it throws them an error saying they’re missing. But in my case, I wanted to change it up and to not get the user to that point.

I want to “disable” the button if the text field is NOT filled in.
Whenever they choose an option or fill in the field, then would I want to “enable” the button to be clicked.

Please give some advice/tips on how I can go on about this.

Showing Posts 1 to 10

NobbZ

NobbZ

As with every other server side framework as well, you’ll have to use JavaScript for that.

The script is probably identical to one you would use for ruby on rails or one for Django…

Well, maybe you could use live view as well, but I think that would overcomplicate things.

rennz555

rennz555 OP

So there’s really no way to disable search buttons until input is selected using elixir in the template?

NobbZ

NobbZ

That’s what you can do with live view, but either way, none of the versions work when JS is disabled on the client.

lucaong

lucaong

Elixir, like other server side languages, can render the page’s HTML and send it to the browser, but once the page “leaves the server” and gets to the browser, Elixir has no control anymore (one exception to this is LiveView, but I doubt it is the right solution to your problem). Thus, Elixir can render the button as disabled (e.g. by rendering it as <button disabled>...</button>) but it cannot observe the user interaction with the browser to re-enable it when necessary. The language that runs in the browser and can do that is JavaScript. You would set an event listener on the search input, and toggle the “disabled” attribute on the button depending on whether the input value is blank or not.

rennz555

rennz555 OP

Can you give an example on how I can do it in live view?

persay,

<%= if text_field == nil do %> 
      <%= submit "Search" %>
<% end %>

Sorry, I’m a beginner in Elixir/Phoenix.

rennz555

rennz555 OP

I see. I totally understand now. Thanks for your help.

In this case, I can use JS in an Elixir/Phoenix Project?

I’m a total beginner at using this language and framework so sorry for questions.

NobbZ

NobbZ

Sorry, I have not used live view yet.

Yes, the easiest us to just inline it into the page you are rendering, for such a thing probably the easiest solution.

The other way is to put it I to your app.js and let brunch do the packaging stuff…

lucaong

lucaong

Hi @rennz555, no need to be sorry for your questions. It’s awesome that you’re moving your steps into Elixir :slight_smile: we all asked our fair share of beginner questions

Yes, you can definitely use JavaScript with Elixir and Phoenix. As a matter of fact, any web framework eventually produces HTML, CSS and JavaScript, as those three are the only languages understood by the browser. You can either add JavaScript “inline” in your templates with a <script> tag, or place your JavaScript files in the assets folder and require it in your templates.

OvermindDL1

OvermindDL1

If you craft your forms well you can use CSS to require the preceeding inputs or form itself to be in a valid state before it even is visible or something (can swap with another disabled button that is then hidden when the form is valid), all with no JS or live_view or anything. ^.^

rennz555

rennz555 OP

Hey lucaong, (this is just a theoretical question.)

So what if it’s not a text area input, but rather a drop down category list? Once a user chooses a category, the button will remain “enabled”, but initially the button is “disabled”. Rather than getting an event listener to keep track of the search input checking whether the input value is blank or not.

Main concern is since having a category drop down menu, it will never go blank again and the only other choice is to choose a category if you want to switch it.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews