1

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 ?

4 Answers 4

2

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);
        });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Your methodology is correct, though the OP is not "improperly" calling the script from his server. The .load() method, as I understand it, is more or less identical to the .get() method, except that it has an implied callback that adds the response to the inner HTML of the element that .load() was called on. So, the script was likely being retrieved, but never added to the input element's value attribute. Had this code been applied to a div, it probably would have worked (in as much as the response would have made it to the DOM).
Yes I apologize. You are right, had it been applied to a div it would have worked since .load would add whatever html came back upon loading the script. I should have clarified that if he wants to get the data from a script and replace a current value of an input, he would have to use the $.get or $.getJSON methods.
0

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()

Comments

0

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);
    });

Comments

0

.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);
     }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.