Caio Felipe
Caio Felipe

Reputation: 81

how to set the value to a input hidden field from value entered in another input text field Javascript

I want to get this form name and put in form name hidden

<input placeholder="Nome" class="form-control" type="text" 
name="morning[name]" 
id="morning_name">

hidden field

<input placeholder="Nome" hide_label="true" required="required" 
type="hidden" 
name="morning[foots_attributes][0][name]" 
id="morning_foots_attributes_0_name">

my bootstrap_form

<%= bootstrap_form_with(model: @morning, url: morning_path, local: true) do |form| %>

<%= form.text_field :name, placeholder: "Nome", hide_label: true%>

<%= form.fields_for :foots do |foot| %>
<%= foot.hidden_field :name, placeholder: "Nome", hide_label: true, required: true %>   
<% end %>

<%= form.submit "Registrar horário", class:"btn btn-primary", data: { disable_with: 
'Registrando....' } %>

<% end %>

<%= javascript_pack_tag 'custom' %>

Upvotes: 0

Views: 228

Answers (1)

Aaron Boult
Aaron Boult

Reputation: 256

You can use the jQuery .change() to accomplish this.

$("#morning_name").change(function(){
    $("#morning_foots_attributes_0_name").val(this.value);
});

https://api.jquery.com/change/

Upvotes: 1

Related Questions