I have multi html block with same code like this:
<div id="up1">
<input id="sub1" />
</div>
<div id="up2">
<input id="sub2" />
</div>
.
.
.
<div id="upn">
<input id="subn" />
</div>
for each html these block I have same jquery code (with own id for each of them)
like this:
$(document).on("change", "#up1",function() {
e.preventDefault();
... some code ...
}));
$(document).on("change", "#up2",function() {
e.preventDefault();
... some code ...
}));
.
.
.
$(document).on("change", "#upn",function() {
e.preventDefault();
... some code ...
}));
this is my qestion
how to avoid repeating code?..
maybe u suggest me I must use class instead id,But it's not my answer.I want use only id.
I must use id,not class... because I want print result (or send data) on it's block.
seems I must use a function, but how can I call them in a function?
for example is this valid?
function myfunction(id1,id2) {
$(document).on("change", "#up1",function() {
e.preventDefault();
... some code ...
}));
}
myfunction('up1','sub1');
myfunction('up2','sub2');
myfunction('up3','sub3');
id. Common misconception issue when learning but there are numerous ways to traverse within instances of a class