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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to &lt;%= web_namespace %&gt;.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews