Q:

How to get input value into ajax request

I'm don't know how to get the value from the textbox into my search query, for the API I'm using.
In this example I want to get information about London, but how can I replace London with the value from the city text-input.
$.getJSON('https://api.search?near=LONDON&v=20120101',
<body onLoad="init()">
Search the place: <input type="text" id="city" class="cjtxt"></input>

I tried to do the following:
$.getJSON('https://api.search?near=+city&v=20120101', function(){});

But with no success.. What am I doing wrong, and how can I solve this?
Any idea?

A:

Most simple version
You add an event listener to the input field, and every time it changes, you then collect the value, and put it into the Ajax Request.
$("#city").change(function(){
    var city = $(this).val();
    $.getJSON('https://api.search?near=' + city  + '&v=20120101', 
        function(data){
        // Your data will be available here
    });
});

Added functionality
You should guard the request with some sort of validation logic, fx. it can't be less the 2 characters long, or only be requested when the user has stopped typing for 500 milliseconds.
Exmaple #1 - Minimum 2 characters:
$("#city").change(function(){
    var city = $(this).val();
    if(city && city.length >= 2){
        $.getJSON('https://api.search?near=' + city  + '&v=20120101', 
           function(data){
            // Your data will be available here
        });
    }
});

Exmaple #2 - Request after user stopped typing(500ms):
var cityTimeoutId;
$("#city").change(function(){
    // Stop the 'Timeout' if it haven't already been fired
    clearTimeout(cityTimeoutId); 
    var city = $(this).val();
    // Set the timeout, meaning it will run this method in 500ms
    cityTimeoutId = setTimeout(function(){
        $.getJSON('https://api.search?near=' + city  + '&v=20120101', 
            function(data){
            // Your data will be available here
        });
    }, 500);//<- The 500ms is set here, as the last param of the timeout function
});

