mardi 28 mars 2017

How can I make a js function recognize a clicked or empty checkbox?

I'm modifiying an existing form.

You can launch a task block with several fields, and if these are all empty, you can remove the block again. Most of them are required, to submit as data complete. If not complete, you can save Work in Process (sic! I'd have preferred Work in Progress...)

For the code to recognize empty fields, it seems that most of them have value="". When completed, the system seems to to recognize that they are not empty.

But the Checkbox I want to use causes problems.

If I don't set value="", it's not recognized as empty, and I can't remove the block.

If I set value="" , it's not recognizing an entered checkmark, and I can't submit for Data Complete.

I thought that onClick="...." and defining a way to set value="true" would be a way forward, but haven't found any example while searching, and being quite the beginner, I haven't learned it all yet.

the checkbox (to be renumbered up to id 050):

<label for="completed_001">Task Completed<em class="labelrequired">*</em></label>
<input type="checkbox" id="completed_001" name="completed_001" alt="Task Completed 001" title="Task Completed 001" value="" class="validate['required']">

the function: (where the last row concerns the checkbox)

function isEmptyAction(nr) {

    var pad = "000";
    var nr = (pad+nr).slice(-pad.length);

    return  document.getElementById('taskdescription' + nr).value == '' &&
            document.getElementById('TaskOwner' + nr).value == '' &&
            document.getElementById('taskduedate' + nr + '_date').value == '' &&
            document.getElementById('documentid' + nr).value == '' &&
            document.getElementById('resultsandcomments' + nr).value == '' &&
            document.getElementById('completed_' + nr).value == '';  
}

= = = = = UPDATE = = = =

1) thanks for the edit improvement, fellow user dmorrow!

2) Thanks for the tips and suggestions, I got it to work eventually!

I removed the value="" from the checkbox code in the html. This allows the entered checkmark to be recognized, when required for sumbmitting Data Complete.

I used document.getElementById('completed_' + nr).checked == false; in the function for checking that all fields are empty. This allows removing the task block when empty.

Thanks again! You made me a happy beginner!




Aucun commentaire:

Enregistrer un commentaire