473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ruby check_boxes - check all/uncheck all with Javascript

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

Apr 25 '06 #1
1 2722
wa**********@gmail.com wrote:
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>

Apr 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
10
by: Neal | last post by:
I'm beginning some experiments with Ruby in XHTML 1.1. I'm finding very odd results which surprise me. I'm using a PHP snippet which serves application/xml+xhtml and XHTML 1.1 to those browsers...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
5
by: kid | last post by:
Hi, I have 5 checkbox elements, i.e: ( 1 ) a ( 2 ) b ( 3 ) c ( 4 ) d ( Check/Uncheck all )
12
by: swebster | last post by:
I need to zero in on a specific section of my jsp form during a hide to uncheck the checkboxes in that section. Currently, anything I try either unchecks all checkboxes on the form or doesn't work...
23
by: kigoobe | last post by:
Hi friends, I'm having a menu like this - http://www.kigoobe.com/offshoring/bugs/javascript/treeMenuCheckToggle.gif that I've fetched from the database, using recursive function, where data...
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.