kndoshn
kndoshn

Reputation: 21

Showing alerts using form section value

How can I use the selected value in form for alerts?

Is there a way to get it by getElementById or equivalent?

Form

= f.select :some_key, some_array
`= f.submit 'Submit', data: { confirm: t('views.confirm.caution', :choice => some_array[<CHOSEN INDEX HERE>]) }`

en.yml

en:
  views:
    confirm:
      caution: "Are you sure to submit %{choice} ?"

alert

Upvotes: 0

Views: 134

Answers (1)

en.yml

en:
  views:
    confirm:
      caution: "Are you sure to submit {{choice}} ?"

form.slim

= f.select :some_key, some_array
- # other code
= f.submit 'Submit', data: { confirm: '' ) }`

:javascript
  document.getElementById('your_select_id').addEventListener('change', function(){
    var value = '' // get select value
    var btn = document.getElementById('you confirm button id')
    var original_message = "#{t('views.confirm.caution')}"
    var confirm_message = original_message.replace('{{choice}}', value)
    btn.setAttribute('data-confirm', confirm_message)
  })

Upvotes: 0

Related Questions