Reputation: 2463
I'd like to have a custom select field with 3 options: dog, cat, other - and when the other is selected I'd like the blank text input to appear below to allow user to write his favourite animal in it.
Is there a way to conditionally render field based on option selected?
<%=
select f, :animal, [
{"Dog", :dog},
{"Cat", :cat},
{"Other", :other}
]
text_input f, :animal
%>
Upvotes: 0
Views: 329
Reputation: 5148
You can't achieve it without the help of JavaScript.
HTML tags are rendered on the server side and the choice of option takes place on the browser.
If you don't want to write JavaScript code, try Phoenix LiveView, which allows you to change the DOM tree dynamically on the server side.
A JavaScript library works behind the Phoenix LiveView, but you will hardly notice it.
Upvotes: 2