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

JavaScript

nicholas maddren
nicholas maddren
12,793 Points

Parsing SQL data based on the 'Make' and 'Model'

Hey guys having a bit of a nightmare here, I'm trying to save an option that a user selects in my form however I am unsure how I can achieve this.

For example I have this code:

<select class="form-control select-box">
                 <option value="make-any">Make (Any)</option>
                 <?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC))
                 {
                 echo '
                 <option value="'.$make["Make"].'">'.$make["Make"].'</option>
                 ';
                 } ?>
             </select>

When a user selects one of the options I want it to save the option that has been selected.

How can I do this?

1 Answer

Chris Hubbard
Chris Hubbard
2,253 Points

Nicolas,

Not exactly sure what you mean by 'save', but if you mean you want to access the selected option for use in Javascript, it would go something like this:

// Listen for the form change
$('select.form-control').on('change', function() {
  // Look for selected option
  $('select.form-control option:selected).each(function() {
    // Do something with selected option
    var currentValue = $(this).val();
    alert (currentValue);
  });
});