3

Here is my HTML code (which is i think where the problem is):

<form name="County" method= "post" action="post">
<select id= item.county_name name= item.county_name> 
<option value="item.county_name"> County </option>

</select>
</form> 

Here is my javascript:

$.ajax({
url:'http://107.170.75.124/courts/courts.json',
method: 'post'
})
.done (function(data){
   data.forEach(function(item){
    console.log (item.county_name);
     $ ('select'). append ('<option value="' + item.county_name + '">' + 
'</option>');


})
}); 

When I attempt to run it the drop box shows all 100 areas where they should be a county, but they are blank. So my guess is that there is something wrong with the HTML preventing it from being visible. All of the counties show up in my console

2 Answers 2

3

You are forgetting to supply the actual display value of the <option> elements. Your <option> elements are winding up like this:

<option value='ajax result item here'></option>

With the <option> element being empty.

Change the line to this:

 $ ('select').append('<option value="' + 
     item.county_name + '">' + item.county_name + '</option>');

Or, just this (because when no value is explicitly set on an option, the value becomes the displayed text of the option):

 $ ('select').append('<option>' + item.county_name + '</option>');
Sign up to request clarification or add additional context in comments.

Comments

0

The "value" param just sets the value of the option, but not it's label. The label needs to be between the <option>tags

Your code is incomplete.

$ ('select').append('<option value="' + item.county_name + '">' + '</option>');

results in something like this

<option value="somecountry"></option>

You need to change it like:

$ ('select').append ('<option value="' + item.county_name + '">' + item.county_name + '</option>');

which should output

<option value="somecountry">somecountry</option>

1 Comment

You have a syntax error in your suggested code fix. Once you fix that, you will have the same code that I posted an hour and a half earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.