Reputation: 21
How can I use the selected value in form for alerts?
Is there a way to get it by getElementById
or equivalent?
= f.select :some_key, some_array
`= f.submit 'Submit', data: { confirm: t('views.confirm.caution', :choice => some_array[<CHOSEN INDEX HERE>]) }`
en:
views:
confirm:
caution: "Are you sure to submit %{choice} ?"
Upvotes: 0
Views: 134
Reputation: 172
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