Ruby check_boxes - check all/uncheck all with Javascript

W

walkerhunter

I have the three embedded Ruby(erb) check_boxes below:

Patient #1<%= check_box("patient", '1', {}, "yes","no") %></br>
Patient #2<%= check_box("patient", '2', {}, "yes","no") %></br>
Patient #4<%= check_box("patient", '4', {}, "yes","no") %></br>


The HTML output looks like this:

Patient #1<input id="patient_1" name="patient[1]" type="checkbox"
value="yes" /><input name="patient[1]" type="hidden" value="no" /></br>

Patient #2<input id="patient_2" name="patient[2]" type="checkbox"
value="yes" /><input name="patient[2]" type="hidden" value="no" /></br>

Patient #4<input id="patient_4" name="patient[4]" type="checkbox"
value="yes" /><input name="patient[4]" type="hidden" value="no" /></br>


I want to create a button(s) to check all/uncheck all the check_boxes
if clicked by the user. I was thinking I had to use javascript to do
this, but the erb is appending the value to the name in the HTML
output. What this does is prevent my javascript from recognizing these
check_boxes as a collection named "patient".

I know this isn't a pure javascript question, but I am at a loss. I am
new to both languages, which doesn't help either.

Thank you for your time!

-Hunter
 
P

petermichaux

I have the three embedded Ruby(erb) check_boxes below:

Patient #1<%= check_box("patient", '1', {}, "yes","no") %></br>
Patient #2<%= check_box("patient", '2', {}, "yes","no") %></br>
Patient #4<%= check_box("patient", '4', {}, "yes","no") %></br>

I think you need to remove one of yes or no. I don't know why you would
want the hidden input below.


The HTML output looks like this:

Patient #1<input id="patient_1" name="patient[1]" type="checkbox"
value="yes" /><input name="patient[1]" type="hidden" value="no" /></br>

Patient #2<input id="patient_2" name="patient[2]" type="checkbox"
value="yes" /><input name="patient[2]" type="hidden" value="no" /></br>

Patient #4<input id="patient_4" name="patient[4]" type="checkbox"
value="yes" /><input name="patient[4]" type="hidden" value="no" /></br>

I think the following is close to what you want. If you are using
prototype.js in your rails app I have read that it breaks the
javascript for-in loop although I haven't experienced this breakage
even when using prototype.js.

I am not so sure that my regular expressions are the best possible
option but they seem to work here.

The "return false;" makes it so the button calls the checkAll()
function but afterwards does not actually submit the form to the
specified form action.

- Peter


<html>
<head>

<script type="text/javascript">
function checkAll(form, list){
for (property in form) {
if(property.match(new RegExp(list+"\\[\\d*?\\]"))){
form[property].checked = true;
}
}
}

function uncheckAll(form, list){
for (property in form) {
if(property.match(new RegExp(list+"\\[\\d*?\\]"))){
form[property].checked = false;
}
}
}

</script>

</head>
<body>

<form name="myform" action="destination">
Patient #1<input id="patient_1" name="patient[1]" type="checkbox"
value="yes" /></br>
Patient #2<input id="patient_2" name="patient[2]" type="checkbox"
value="yes" /></br>
Patient #4<input id="patient_4" name="patient[4]" type="checkbox"
value="yes" /></br>
<button onclick='checkAll(document.myform, "patient");return
false;'>Check All</button>
<button onclick='uncheckAll(document.myform, "patient");return
false;'>Uncheck All</button>
<input type="submit" value="submit the form" />
</form>

</body>

</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top