Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Radio buttons to toggle div in HAML

I'd like to do something very simple: reveal one div if one radio button is selected, hiding another div, and revealing that div if the other radio button is selected, thus hiding the first div.

This is what I have, which doesn't work:

row
        .col-sm-6.form-group
          = form.radio_button "meta_data[quantity_meta][type]", value: "range", onclick: "toggleDiv('.toggle-range', '.toggle-fixed');"
          = form.label "meta_data[quantity_meta][type]", "range"
      .row
        .col-sm-6.form-group.fixed
          = form.radio_button "meta_data[quantity_meta][type]", value: "fixed", onclick: "toggleDiv('.toggle-fixed', '.toggle-range');"
          = form.label "meta_data[quantity_meta][type]", "fixed"

      .div.toggle-range
        = form.label "YO!", hidden: true

      .div.toggle-fixed
        = form.label "Can't stop the feeling!", hidden: true

And the JavaScript (accessible via asset pipeline):

function toggleDiv(hideDiv, showDiv) {
  $(hideDiv).hide();
  $(showDiv).show();
}

Pretty simple. Yet when I render the page and inspect my "radio buttons", this is what I see:

<input type="radio" value="{:value=>"range", :onclick=>"toggleDiv('.toggle-range', '.toggle-fixed')"}" name="meta_data[quantity_meta][type]">

It's putting my onclick attribute into the value attribute of the radio button.

How can I accomplish my goal here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This has nothing to do with Haml or your javascript. The signature of radio_button is:

radio_button(method, tag_value, options = {}) 

Since you're passing a hash as the second argument it is getting cast to a string. Instead you need:

= form.radio_button "meta_data[quantity_meta][type]", "range", onclick: "toggleDiv('.toggle-range', '.toggle-fixed');"
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda