ndesign11
ndesign11

Reputation: 1779

JSON and jQuery swap json

I have an input field that a user can type in. I'm trying to figure out how I can make the call to json dynamic. So if the person types in printer1, it will use printer1.json. if they type in keyboard5 it will load the keyboard5 json.

Here is my html

<input type="text" value="" class="call-json edit-device" />

here is my jQuery

    $.getJSON('json/printer1.json', function (data) {

        var items = [];
        $.each(data[0].attributes['edgebox.stat.prop.type'], function (key, val) {
            items.push(val);
        });
        displaySortLabel(items, "type-details");

        var items = [];
        $.each(data[0].attributes['edgebox.stat.prop.serial.number'], function (key, val) {
            items.push(val);
        });
        displaySortLabel(items, "serial-number-details");

Upvotes: 1

Views: 75

Answers (1)

Ram
Ram

Reputation: 144719

You can listen to the keyup event.

var timeout = '';
$('.call-json').keyup(function(){
    clearTimeout(timeout);
    var val = this.value;
    timeout = setTimeout(function(){
        $.getJSON('json/'+val+'.json', function (data) {
           // ...
        });       
    }, 80);
})

Upvotes: 2

Related Questions