Jason Chen Jason Chen - 1 year ago 100
Javascript Question

Pass jquery input value to url?

I am able to pass plain text to my api's url as follows:

var bus = $('.location').text();
$.getJSON('http://apiofsite'+bus+'apiofsitecontinued.com'), function(){
do something});

Now I want to be able to do the same thing, but using the input field instead of printed HTML. Something like so:

<input id="stuff" type="input" />

So ideally my JS would be like so.

var sang = $('#stuff').val();
$.getJSON('http://apiofsomewebsite'+sang+'apiofsomewebsitecontinued.com'), function(){
do something});

I don't believe that the URL is reading my input, as it returns empty.

Answer Source

This doesn't seem to have anything to do with the actual ajax call. Try this:

var url = 'http://apiofsomewebsite'+ $('#stuff').val() + 'apiofsomewebsitecontinued.com';
//$.getJSON(url, function() {...});

If the url is not the one you expected, check to see you are placing the code in the right event (i.e. the field's change event, a button's click event, or the form's submission etc.) when the input value is available.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download