h2O h2O - 2 months ago 8
Ajax Question

Load an ajax request in a text box

I want to load an AJAX request in a text box. I tried loading the AJAX request using load :-

$("#textbox-id").load("file.php");


But, it did not work, though the same code worked for a div :-

$("#div-id").load("file.php");


Is there any such function to load the AJAX request in a textbox? Any help will be appreciated.

Answer

Why dont you use the $.ajax method? in success block get the value out of your response data and set it in your textbox

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: 'file.php',
    success: function( data ) {
        $("#textbox-id").val(data);
    },

    error: function(xhr, status, error) {
        alert(status);
    }

});

OR

$.get('file.php', function(data) {
    $("#textbox-id").val(data);
});
Comments