Reputation: 65
I am newbie in javascript, I want to do looping normally use "for". I would like to duplicate this script about 10 ... how loop this script?
function getComboMotif1() {
$.get("file.php?opt1=" + $("#id1"), function (data) {
$("#asd1").html(data);
});
}
The manual Loop script like this !!
function getww1() {
$.get("file.php?opt1=" + $("#id1"), function (data) {
$("#asd1").html(data);
});
}
function getww2() {
$.get("file.php?opt1=" + $("#id2"), function (data) {
$("#asd1").html(data);
});
}
function getww3() {
$.get("file.php?opt1=" + $("#id3"), function (data) {
$("#asd1").html(data);
});
} //and further
Upvotes: 0
Views: 115
Reputation: 679
If you need it to just run through a forloop
, then start at 1
in order to accommodate your name and id.
for(var i = 1; i <= 10; ++i)
$.get("file.php?opt1=" + $("#id" + i), function (data) {
$("#asd1").html(data);
});
Now since get
is asynchronous, each one will not wait for the other to complete.
If you need each function to be created, getww1
and such, then I recommend using eval
to create those functions for you. But, that is very inefficient to do and should be avoided unless there is a specific requirement. Ex:
...
eval("(function () { return function "
+fname+" (){"+
codeAsString
+"};})()"));
...
That will return the newly created function.
I hope this helps to some degree.
Upvotes: 0
Reputation: 3060
Something like that :
function getResource(which) {
$.get('file.php?opt1=' + $('#id' + which), function (data) {
$('#asd' + which).html(data);
}
}
for (var i = 0, max = 3; i < max; i += 1) {
getResource(i);
}
But your code contains a few oddities.
$('#id1')
is a jquery object, so it can't be sent to the server as a string.$('#asd1').html(data)
in each callback, it will get overwritten each time you get an answer from the server. That's why I made it dynamic also.Upvotes: 1