# Javascript in RoR page - Check ALL/uncheck ALL buttons

**URL:** <https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691>\
**Category:** Rails\
**Created:** [April 24, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691 "2006-04-24T22:13:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [April 24, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691/1 "2006-04-24T22:13:58Z")

</div>

I am trying to implement a check all and uncheck all button for my check  
boxes.

Here is what I have below. I am getting this error each time:

Error: document.myform.add\_visit\_for\_this\_Pt has no properties  
Source File: [http://localhost:3000/AddPatientVisit/addvisits](http://localhost:3000/AddPatientVisit/addvisits)  
Line: 1

Strange that I can’t seem to pass value into the function.

Thanks for your help!

  
  
  

Patient #1\<%= check\_box(“add\_visit\_for\_this\_Pt”, ‘1’, {}, “yes”,“no”)  
%\>  
  
Patient #2\<%= check\_box(“add\_visit\_for\_this\_Pt”, ‘2’, {}, “yes”,“no”)  
%\>  
  
Patient #4\<%= check\_box(“add\_visit\_for\_this\_Pt”, ‘4’, {}, “yes”,“no”)  
%\>  
  
Patient #8\<%= check\_box(“add\_visit\_for\_this\_Pt”, ‘8’, {}, “yes”,“no”)  
%\>  
  
Patient #9\<%= check\_box(“add\_visit\_for\_this\_Pt”, ‘9’, {}, “yes”,“no”)  
%\>

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [April 25, 2006, 12:56am UTC](https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691/2 "2006-04-25T00:56:11Z")

</div>

Here is the rendered HTML:

 Some text here1 

Patient #1  
Patient #2  
Patient #4

Patient #8  
Patient #9

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [April 25, 2006, 5:50am UTC](https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691/3 "2006-04-25T05:50:58Z")

</div>

Thank you for your reply, Peter. The check boxes are being renamed by  
the erb, I think. Each checkbox has an ID value which is used to update  
a db (via an api). The erb is appending the value to the name and,  
you’re right, I think that is going to be unrecognizable to the  
javascript code. I need to figure out a way to get those values into  
the params hash which is passed back to the controller.

Is there another way to create these check boxes with erb that won’t  
produce HTML with with the ID appended to the name?

I’lll do as you suggested and post this question at comp.lang.javascript  
also. Thanks again!

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [April 25, 2006, 5:33am UTC](https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691/4 "2006-04-25T05:33:56Z")

</div>

Hunter,

I think there are several of problems.

I think you must put the script tags inside the head or body

Note the use of the type attribute not the language attribute.

Also don’t bother with the trick for hiding javascript from old  
browsers. This isn’t necessary anymore.

For the following why have two elements with the same name? Also why  
have the hidden element at all?

\<input id=“add\_visit\_for\_this\_Pt\_1”

> name=“add\_visit\_for\_this\_Pt[1]” type=“checkbox” value=“yes” /\>

BTW, You might be interested in this page for using tags and attributes  
etc.

> **[HTML Reference](https://www.w3schools.com/tags/)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

If you want to get into JavaScript stuff you might be interested in  
JavaScript The Definitive Guide 4th edition by David Flanagan (O’Reilly)  
to get started.

For your example to work with the Thacker script you cannot use  
“add\_visit\_for\_this\_Pt[1]” etc because when the script searches for  
document.myform.add\_visit\_for\_this\_Pt it will not find anything. There  
isn’t an element or list of elements with the “name  
add\_visit\_for\_this\_Pt”. There is an element with name  
“add\_visit\_for\_this\_Pt[1]”. This is a bit subtle. Study the working  
example below and see if you can make sense if it. I think that  
“add\_vista\_for\_this\_Pt[1]” and “add\_vista\_for\_this\_Pt[2]” are not seen  
by the JavaScript as elements of an array. However if you have multiple  
checkboxes with the name “add\_vista\_for\_this\_Pt” then javascript sees  
them all as part of an array.

Setting the checked attribute to true or false seems to work.

Also this is a perfect question for comp.lang.javascript since this is  
not as much a Rails or Ruby question as it is JavaScript.

I guess you will have to play more with this to get it working the way  
you want to with Rails.

- Peter

Patient #1  
  
Patient #2  
  
Patient #4  
  
Patient #8  
  
Patient #9

 Check All Uncheck All

---

<div class="post-metadata">

**Author:** ![siguva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@siguva](https://www.ruby-forum.com/u/siguva)\
**Post date:** [April 25, 2006, 6:10am UTC](https://www.ruby-forum.com/t/javascript-in-ror-page-check-all-uncheck-all-buttons/58691/5 "2006-04-25T06:10:17Z")

</div>

On 4/24/06, Hunter W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you for your reply, Peter. The check boxes are being renamed by  
> the erb, I think.

Yes

> Each checkbox has an ID value which is used to update  
> a db (via an api). The erb is appending the value to the name and,  
> you’re right, I think that is going to be unrecognizable to the  
> javascript code. I need to figure out a way to get those values into  
> the params hash which is passed back to the controller.
> 
> Is there another way to create these check boxes with erb that won’t  
> produce HTML with with the ID appended to the name?

Actually you want the ID appended by the select\_tag helper because it  
makes processing the submitted data easier. What you probably want to  
do is write new JavaScript that uses a regular expression to find the  
checkboxes of interest.

> I’lll do as you suggested and post this question at comp.lang.javascript  
> also. Thanks again!

They like one question at a time there and try to simplify it as much  
as possible to show you’ve worked on the question as much as you can.

- Peter
