Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda