Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Digital Literacy

Ine Van Severen
Ine Van Severen
7,018 Points

What function do i need to use to get random answer when I wanna use dropdown list instead of input text (MASH game)

Hi,

I wanted to make a list so you can select what you like to eat instead of having to fill it in. But I struggle to track the selected items to use those contents with random_num and it to fill in in the fortune.

Does anybody know how to do this?

Thanks in advance Ine

http://port-80-t71dmgt67d.treehouse-app.com

Steven Parker
Steven Parker
231,275 Points

You can't share a direct URL to your workspace, it's temporary and only exists while you are using it.

But you can use the snapshot function in the workspace and provide the link to that.

1 Answer

Steven Parker
Steven Parker
231,275 Points

I had to fix a few things just to make the code run:

  • there's a stray close brace on line 20
  • the funciton "handle_submission" was defined but not set as the submit event handler

Then I re-wrote "get_answer" to work with the select lists:

function get_answer(category) {
    var select = document.getElementsByName(category)[0];
    var choices = [].filter.call(select.children, f => f.selected).map(o => o.text);
    return choices[random_number(choices.length)]; 
}

You could also do this using a loop, but the main thing was to get the text from the selected items into "choices".