A21
A21

Reputation: 181

How do you pass in the value from collection_select to onchange function in Rails?

I'm trying to pass in the value selected from my collection_select drop down to an onchange function. When I do name, my value prints out as source[index], but I want the value of this not that as the text itself.

<%= collection_select(:source, :index, @sources, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea(name)"}) %>

function updateTextArea(source){
 var value = source;
 console.log(value);
}

Upvotes: 2

Views: 1516

Answers (3)

A21
A21

Reputation: 181

Changing the argument from "name" to "this.value" solved my problem.

<%= collection_select(:source, :index, @sources, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea(this.value)"}) %>

function updateTextArea(source){
 var value = source;
 console.log(value);
}

Upvotes: 2

Ahsan Ellahi
Ahsan Ellahi

Reputation: 336

Try this:

html_options = {:onchange => "updateTextArea()"}

function updateTextArea() {
  console.log($(this).val());
}

Upvotes: 2

fool-dev
fool-dev

Reputation: 7777

I think it would be an example in below:

<%= f.collection_select(:id, index.sources, :id, :name,options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea(name)"})  %> #-> f is a form tag this you use this form

Hope will help you

Upvotes: 0

Related Questions