Reputation: 181
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
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
Reputation: 336
Try this:
html_options = {:onchange => "updateTextArea()"}
function updateTextArea() {
console.log($(this).val());
}
Upvotes: 2
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