I have a function addText(), which has 3 variables (textInput, type, rowID). I declared them without the var keyword (so that they can be used outside the function?). I have many checkboxes which I created this way:
<td><input type="checkbox" name="CB" id="monitor_'+rowID+'"/></td>
Then I have this function, which requires the use of those 3 variables:
function monitoring(rowID, number, type) {
var $check = $('#status_table #monitor_' + rowID);
$('#test').append($check);
if($check.is(':checked')) {
$.post('/request', {
inputText: number,
key_pressed: type
}).done(function (reply) {
if(reply == "on") {
$('#test').append("on");
} else {
$('#test').append("off");
}
});
}
return;
}
This function will be called here:
$('#status_table #monitor_'+rowID).each(function(rowID, textInput, type){
monitoring(rowID, textInput, type);
});
Note: inputText is a variable posted to somewhere.
Questions:
Is this the correct way to call a selector with variable?
$('#status_table #monitor_'+rowID)Should I pass the variables twice in the second set of code (both at the selector function and monitoring function)?
How do I pass variables correctly?
Is the selector correct? Or should I change
$('#status_table #monitor_'+rowID).each(function(){})to$('#status_table tr').each(function(){})?
5. ADDED: where and how should i call the function "monitoring" ? i put it under addText (refer codes below) but it makes no sense because the function wil then be only executed when the "add" button is clicked. Furthermore, as my selector is a variable, i would like to make sure it can actually perform the same thing for all the checkbox that is checked. How can i do this?
I tried but my code has no response at all when I check the checkbox.
EDIT:
Here's my addText() function (I've included function monitoring in this function instead of under another jQuery action as above):
function addText(add_type, fb_type) {
$("#" + add_type + "Add").click(function () {
$('.TextInput').empty();
textInput = $("#" + fb_type + "TextInput").val();
if(textInput.length === 0) {
alert('please enter the fieldname');
return;
}
index = $('#status_table tbody tr').last().index() + 1;
type = $("#select-choice-1").find(":selected").text();
rowID = type + textInput;
var str = '<tr id="' + rowID + '">' + '<td>' + index + '</td><td>' + rowID +
'</td><td class="type_row_' + textInput + '">' + type + '</td><td class="feedback number">' +
textInput + '</td>' + '<td><img src="static/OffLamp-icon.png" class="image" id="off"></td>' +
'<td><input type="checkbox" name="CB" id="monitor_' + rowID +
'" class="custom" data-mini="true" /><label for="CB"> </label></td><td class="outputRemove">x</td>' +
'</tr>';
if(alreadyExist(textInput, type)) {
alert('Entry exists. Please enter another number.')
} else {
$('#status_table tr:last').after(str);
}
monitoring(rowID, textInput, type);
return;
});
}
The HTML of the table:
<table class="config" id="status_table">
<thead>
<tr>
<th colspan="4" ; style="padding-bottom: 20px; color:#6666FF; text-align:left; font-size: 1.5em">Output Status</th>
</tr>
<tr>
<th>Index</th>
<th>Row ID</th>
<th>Feedback Type</th>
<th>Feedback Number</th>
<th>Status</th>
<th>Monitor?</th>
<th>Remove?</th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
.each(). api.jquery.com/each.each()call would be helpful; if that's inside theaddText()function then post the whole of that (or strip it to the relevant parts).