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

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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews