karlihnos
karlihnos

Reputation: 425

Getting value with Jquery in Rails

I have tried many things but without success.

I want to get the value introduced by the user (user_entry) when clicking on the button (ddd).

This is what I have:

My index.html.erb

<div >
  <p class="mod1_title">Intro</p>
  <div class="mod1_boxed">
    <strong>Jahreshausverbrauch (kWh):</strong>
      <%= form_tag( '/welcome/index', post: true ) do %>
      <%= text_field_tag "module1", nil, placeholder: "Bsp. 3500", id: "user_entry" %></br>

      <strong>PV-Große (kWp):</strong></br>
      <%= text_field_tag "module2", nil, placeholder: "Bsp. 5", id: "user_entry_2" %></br>
      <%= submit_tag "send", id: "ddd" %>

      <%end%>
  </div>  
</div>


<script >

$(function(){


        $("#ddd").change(function(){ 
        var number_value =  $(this).val("#user_entry_module1"); 
         console.log(number_value); 
         });
});

</script>

Any idea?

Upvotes: 1

Views: 1859

Answers (2)

rick
rick

Reputation: 1705

Use click event on button instead of change event,

<script type="text/javascript">

$(document).ready(function(){
     $("#ddd").click(function(){ 
       var number_value =  $("#user_entry").val(); 
       console.log(number_value); 
     });
  });

</script>

Upvotes: 1

Satpal
Satpal

Reputation: 133453

Use

//Its button so use click event
$("#ddd").click(function(){

    //Id is user_entry not user_entry_module1
    //You need id selector to find element the use .val() method to read its value
    var number_value =  $("#user_entry").val(); 

    console.log(number_value); 
});

References:

Selects a single element with the given id attribute.

Description: Get the current value of the first element in the set of matched elements.


Use

$("form").submit(function(){

    //Id is user_entry not user_entry_module1
    //You need id selector to find element the use .val() method to read its value
    var number_value =  $("#user_entry").val(); 

    console.log(number_value); 
});

Upvotes: 0

Related Questions