Document.myform.checkbox.checked Without A Form
Solution 1:
Using jQuery, this could be done very succinctly.
// select all checked checkboxes with name attribute that starts with 'iphone'
var NewCount = $("input[name^='iphone']:checked").length;
if (NewCount > 1){
descontox = 20/100;
valorx = (total.value * descontox).toFixed(2);
valor.value = (total.value - valorx).toFixed(2);
}
if (NewCount <= 1){
valor.value = ("");}
}
EDIT: souza -- It appears based on your comments that you have already solved the issue by using a single form element. However, you would be wise to use a more generic approach like the one above, or the one posted by mrtsherman. Checking each individual input element by name will lead to a code maintenance nightmare.
It would require updating both the your markup and your javascript every time a new iPhone version and/or carrier, platform comes around. A more generic approach would allow you to simply add a new checkbox to the markup using the same naming convention and your javascript will simply handle it without the need to make any changes to the code.
Solution 2:
Use Jquery. Easy to use for something like this. Here is a jsfiddle with it working.
$(document).ready(function() {
$("input:checkbox").click(function() {
var checked = $("input:checked").length;
alert(checked);
});
});
Post a Comment for "Document.myform.checkbox.checked Without A Form"