How to extract input ID in rails to use it in javascript

I'm trying to show text input field depending on checkbox checked value. Rails form helpers generate inputs with automaticly set ids. How can I refer to them in javascript.

Fragment of rails form

<%= f.check_box_without_bootstrap :standard_product%>
<%= f.label(:standard_product, "Standard product" %>
<%= f.text_field :product_comment%>

Javascript function

<script type="text/javascript">
  var checkbox = document.getElementById('????');
  var details_div = checkbox.nextSibling.nextSibling;
  checkbox.onchange = function() {
    if(this.checked) {
      details_div.style['display'] = 'block';
    } else {
      details_div.style['display'] = 'none';
    }
  };
</script>

Could you please suggest solution (that is not hard setting id for checkbox)? Thanks :-)


Thank you very much. I just copied generated id from inspector...

I wish to know how to point to id generated by ruby helper f.check_box in <script> tag using <%= ruby %> :-)

Upvotes: -1

Views: 305

Answers (1)

rks
rks

Reputation: 185

Rails check_box helper is defined as:

check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") public

so in your code you can pass id in the options

Upvotes: 0

Related Questions