Forum Moderators: open
I have it setup now that they should just enter SAB in a field if it is the "Same As Above"
I would like to put a checkbox next to each field that they can click and will enter SAB in the text field that is next to it.
I have seen some codes to do this but I don't know if I have to create 85 different javascript functions (the number of fields that would have a checkbox next to them for the week) or if there is simpler way to do it. (Click a checkbox and the text field immediately preceeding it becomes filled with "SAB")
Thanks for your help guys.
This is what I am using now:
<script type="text/javascript">
window.onload = function () {
// Attach event handlers
var cb1 = document.getElementById('thecheckbox1');
var thebox1 = document.getElementById('thebox1');
cb1.onclick = function () {
if (cb1.checked) {
thebox1.value = 'SAB';
}
thebox1.readonly = cb1.checked;
};
};
</script>
<input type="text" name="Monday800" id="thebox1" size="20" value="<?php echo $row['Monday800']; ?>"/><input type="checkbox" id="thecheckbox1">
You can save yourself from writing 85 different javascript functions just by passing a single parameter to one, reusable function. Here's the basic idea:
function fillInput(elm){
var the_id = elm.id.replace(/[^0-9]+/, ''); // extract the number from the thecheckbox id
if(elm.checked){
document.getElementById('thebox'+the_id).value='SAB';
} else {
document.getElementById('thebox'+the_id).readonly=elm.checked;
}
}
Then, in your onload code, you just loop through all the checkboxes and attach that function to them. :)