Reputation: 9711
I have some input like:
'<input type="text" name="display-count" class="display-count" value="23" autocomplete="off" readonly="readonly" />';
And I'm using this jQuery code to remove the value it has and replace it with another one:
var count = $('form.user-count-form').find('.display-count');
$(count).val('');
$(count).load("admin/user-count.php");
But the new value it isn't loaded. I have tried some other ways like:
$(count).val().load("admin/user-count.php");
or
$(count).val(load("admin/user-count.php"));
But didn't work as well. How do I do what I want ?
Upvotes: 1
Views: 6330
Reputation: 199
.load() will return the Html Contents to matched Element ,it wont set any value. Better you can use $.get or $.post or $.ajax
$.get('admin/user-count.php', function(resultdata){
$('input.display-count').val(resultdata);
});
or
$.ajax({
url: 'admin/user-count.php',
success: function( resultdata ) {
$(count).val(resultdata);
}
});
Upvotes: 0
Reputation: 2750
why dont your just try this
$('input.display-count').load("admin/user-count.php);
or try this onLoad of your HTML doc
$.get('admin/user-count.php', function(response){
$('input.display-count').val(response);
});
Upvotes: 0
Reputation: 97
depends of what your script returns
you can do it so, for example:
$.ajax({
url: 'admin/user-count.php',
success: function( data ) {
$(count).val(data);
}
});
In additional you can return you user-count in JSON format, using php-function json_encode()
Upvotes: 0
Reputation: 616
You need to use the JQuery get method here. You are improperly calling the script from your server and therefore, no call is actually firing and no data is being returned. I'm assuming that the value you are getting is a single string of either text or numbers.
the question is a bit vague so I'm not sure why you have the initial value set at 23 and I'm also not sure when you want to change the value so I am going to show you how to do it upon the dom being ready using the $(document).ready method.
$(document).ready(function(){
$.get('admin/user-count.php', function(data){
$('.display-count').val(data);
});
});
Upvotes: 2