Reputation: 2177
I want to display a value when page is loaded.
My code is,
$.getJSON("http://***.com/Test/testnew.php", function (data) {
$.each(data, function (i, sample) {
var dd = data.sample;
alert(dd);
var count = dd,
pad = '00000';
$('#support_num').click(function (e) {
e.preventDefault();
var ctxt = '' + count;
$('p').text(pad.substr(0, pad.length - ctxt.length) + ctxt);
});
});
});
Here the value is displayed onclick of a button. But i want it on page load.
Upvotes: 1
Views: 572
Reputation: 707686
Here's a modified version of your code that runs when the page loads instead of on the click:
$(document).ready(function() {
$.getJSON("http://www.sample.com/Test/testnew.php",function(data) {
$.each(data, function(i, sample) {
var count = data.sample + "";
var pad = '00000';
$('p').text(pad.substr(0, pad.length - count.length) + count);
});
});
});
The part of this code that doesn't make sense is running the same code multiple times in the $.each()
loop. That is probably not doing what you want, but I left it that way because you haven't said how it's really supposed to handle multiple pieces of data in the response.
Upvotes: 2
Reputation: 93
You can achieve this by putting your function in Document.Ready like
By doing this ur function will be called when the DOM is loaded, and u will get the alert.
enter code here
$(document).ready(function(){
// Your code goes here.
});
Upvotes: 1