How can I pass arguments to a function that is assigned to variable. For example:
var updateDiv = function() {
var row = this;
var values = "";
$('input[type=text],textarea,input[type=radio]:checked,input[type=checkbox]:checked', this).each(function() {
if ($(this).val()!="" && $(this).val()!=null) {
if (values!="") values = values + ","+ $(this).val();
else values += $(this).val();
}
});
if (values!="") {
if(values.substring(0,1)==",") values = values.substring(1) +"<br>";
else values = values +"<br>";
}
$('.jist', row).append(values);
}
$('tr:has(input)').each(updateDiv);
$('tr:has(textarea)').each(updateDiv);
HTML:
<tr>
<td>ai</td><td> <input type="text" name="ai" id="ai"></td>
<td><input type="checkbox" name="ana" id="ana" value="N/A"></td>
<td><div class="jist"></div></td>
</tr>
I want to pass arguments to updateDiv -> updateDiv("mystring");
and I want to use "mystring" in the function this way - > $('.'+mystring, row).append(values);
$('tr:has(input)').each(updateDiv("mystring"));and modify my function's last line to$('.'+mystring, row).append(values);it's throwing an error: TypeError: c is undefinedeach()method, so thateach()can call that function. The term for this is callback. The question is: What value do you want to pass to the referred function? Your function has an empty parameter list, and you are not usingargumentsin the function body. So whatever this function is passed, it does exactly the same.