[LRUG] Stumped: Dynamically showing and hiding a dropdown based on a previous dropdowns selection?

Stuart Harrison pezholio at gmail.com
Mon Nov 21 08:37:08 PST 2016


Hey Jesse

The first issue is because the #secondary div isn’t present in the DOM. The best way round this is to use `find` (https://api.jquery.com/find/). That way you can do $(some-containing-selector).find(‘#secondary’).hide() or $(some-containing-selector).find(‘#secondary’).show(). 

As for the second part, yeah, AJAX is your best bet there. You can then render some JSON in your Users#index or Products#index controller and go from there.

Hope this helps!

Cheers
-- 
Stuart Harrison
Sent with Airmail

On 21 November 2016 at 16:23:03, Jesse Waites (jesse.waites at gmail.com) wrote:

Hi all,

I wanted to run this by you. I have a project wherein I have a dropdown inside of a modal. Lets say it says "Users" and "Products". (It is more complex and more than 2 options but this example gets the point across)

If the user selects "Users", I then need another dropdown to appear below it that contains all of the users, so one can be selected. If the user selects "Products", that dropdown is populated with all of the products.

In getting this started, I have the secondary dropdown in a div, lets call it <div id="secondary">

If I open the console, I can hide and show it with $('#secondary).hide(); or $('#secondary').show();
but I can't seem to get it working inline, in a script tag, on the page. At this point I'm not sure if I need to chase down an asset pipeline issue, or perhaps it isn't working due to the fact it is inside a modal and not immediately rendered. I'm kind of stumped and this is the first time I've done something like this.

After I defeat this issue, I'll need to populate that dropdown, and I guess I will use AJAX for that? Am open to any advice or input there as well. I'll basically call Users.all or Products.all, but I don't want to hit the database for that until the initial selection takes place... the user might not select ANYTHING from that first dropdown and so would be wasteful to make DB calls at that point.

Thank you very much for your time and advice,

--
Jesse Waites
Web Developer
JesseWaites.com
_______________________________________________  
Chat mailing list  
Chat at lists.lrug.org  
Archives: http://lists.lrug.org/pipermail/chat-lrug.org  
Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org  
List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20161121/442735c9/attachment.html>


More information about the Chat mailing list