0

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

1 Answer 1

0

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)
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.