Populate a dropdown with text file and dynamically update the second drop down


#1

I have a situation here.

I created two drop downs, where the second one dynamically updates according to first selection.

Now I jus have 2 members in first drop down and 20 in second drop down. I hard coded using javascript. But by the end of this year the number increases to 100's, then we cannot hard code it.

I use html,javascript,jsp for the webpage.

Is there a way to populate the dropdowns with data in a text file where they can update the text file with new members.

Please help me out and make my situation better!!!! frowning


#2

What data format will you want to be using for the text file?

Something like JSON?

[
    { id: 158377, name: 'Paul Wilkins' },
    { id: 430682, name: 'harshasrujan' }
}

Or perhaps native HTML, which makes some parts easier, but manipulating it harder.

<option value="158377">Paul Wilkins</option>
<option value="430682">harshasrujan</option>

If you use a different data format, have you considered how you will get that data in to javascript?


#3

These are list of regions, bankers. Just Strings. There is no particular format for data. According to the solution I come up, they can provide the data in any required format.

I am sorry, I dont what JSON is.

Thanks for reply!!!!


#4

JSON is JavaScript Object Notation.

Basically you can read in JSON data from any kind of external source, and parse it to actual data.
That way you can then loop through the array and then process it as actual data.


#5

Ok. what format the data should be in?

How can we read the external source like text file?


#6

The format that it's in entirely depends on how it's going to be used.

Have a look at the [example code from the [url="http://bulletproofajax.com/"]Bulletproof Ajax](http://bulletproofajax.com/code/) book.

The chapter 3 code is a good simple example of the type of coding you would use to retrieve the contents of a text file.