December 14

0 comments

Parenthesis in a Rails radio_button_tag was preventing onclick javascript from working.

By Christopher Mendla

December 14, 2017


Last Updated on November 29, 2019 by Christopher G Mendla

I was working with some existing code where I needed to add an onclick javascript event.

I couldn’t even get an alert box to work. After a good bit of trial and error, I found the reason. I put in a line of radio button code from the www3 school’s try it and it worked. After closer inspection, I found that the original radio button had parenthesis.

The original code was

<%= radio_button_tag( :data_file, “cars”)”%>

I tried adding the onclick as

<%= radio_button_tag( :data_file, “cars”, false, onclick: “setFileType(‘Cars’);”)%>

That didn’t work. Based on the sample line of code that did work, I removed the enclosing parenthesis as follows.

<%= radio_button_tag :data_file, “cars”, false, onclick: “setFileType(‘Cars’);”%>

Now the code and developer (Me) are happy!!

Christopher Mendla

About the author

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}