0

I have a javascript autocomplete. The autocomplete function works. I have a php script where it pulls 'subtypes; from a 'type' in mysql and results correctly. but i can't seem to get javascript to pass on the 'type' value (dropdown) if it has changed after the page lode. How can i get this function to work correctly if I change the 'type' dropdown element in the form after the page load?

The Javascript:

<script>
$(document).ready(function(){
var e = document.getElementById("type");
var type = e.options[e.selectedIndex].value;
 $("#subtype").autocomplete("autocomplete/subtype.php?type="+type, {
        selectFirst: true
    });
});
</script>

The HTML:

<select id="type" name="type"> 
        <option value="option1" selected="selected">option1</option>
        <option value="option2">option2</option>
        <option value="option3">option3</option>
        <option value="option4">option4</option>

    </select> </p>
     <p><input type="text" name="subtype" id="subtype" value="" placeholder="Sub-Type"></p>

Thank You for your help. I'm new to javascript.

Sparky
  • 98,165
  • 25
  • 199
  • 285
varcor
  • 63
  • 1
  • 2
  • 10
  • You need a change listener on the select element. You can also just do `var type = e.value` or the jQuery-esque `$('#type').val()` if you want to consume a bunch more CPU cycles. – RobG Oct 02 '14 at 23:18
  • That didn't work. I changed the dropdown and the autocomplete functions that came up were from the original selected option. – varcor Oct 02 '14 at 23:28
  • possible duplicate of [jquery get selected option from dropdown](http://stackoverflow.com/questions/10659097/jquery-get-selected-option-from-dropdown) – Sparky Oct 02 '14 at 23:28
  • I tried the $('#type').change(function() { var type = this.options[this.selectedIndex]; }); and that didn't seam to work either. Not sure why – varcor Oct 02 '14 at 23:41
  • @varcor—you need to pass the value: `var type = this.value` or `var type = $(this).val()` but I very much prefer the first. – RobG Oct 03 '14 at 05:59

0 Answers0