/    Sign up×
Community /Pin to ProfileBookmark

Selectbox choice shows content from json

Hello,
I would like to ask you dear helpers how can I make not only selectbox with ajax content changing but also this content should be loaded from JSON.

I have
<code>
<form id=”id3″ method=”post” action=””><div><input type=”hidden” name=”” id=””></div>
<p>option</p>
<select name=”cID”>
<option selected=”selected” value=””>— out from choice —</option>
<optgroup id=”idx”>
</optgroup>
</select>

<div><input id=”button” type=”submit” value=”search”></div>
</form>
<div id=”content”></div>

<script type=”text/javascript”>
$select = $(‘#idx’);
$.ajax({
url: ‘jsonfile.json’,
dataType:’JSON’,
success:function(data){
$select.html(”);
$.each(data.stations, function(key, val){
$select.append(‘<option id=”‘ + val.cID + ‘”>’ + val.cN + ‘</option>’);
})
},
error:function(){
$select.html(‘<option id=”-1″>none available</option>’);
}
});

$(‘#id5’).bind(‘change’, function() {
var selectbox = $(‘#id5’).val();
var myExp = new RegExp(selectbox, “i”);
$.getJSON(‘../../sys/ws/timetables.json’, function(data) {
var output = ‘<ul>’;
$.each(data, function(key, val) {
if (val.stationId.search(myExp) != -1) {
output += ‘<li>’;
output += ‘<h2>’+ val.stationId +'</h2>’;
output += ‘<p>’+ val.stationName +'</p>’;
output += ‘</li>’;
} else {
return console.log(“Error in ajax”);
}
});
output += ‘</ul>’;
$(‘#content’).html(output);
});
});
</script>

</code>

Thank you very much.
Greetings, maestorm.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootMay 23.2016 — Your code is not JavaScript, it is JQuery, please ask in the right forum please.
Copy linkTweet thisAlerts:
@TrainMay 23.2016 — Moved thread.
×

Success!

Help @maestorm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...