Reputation: 10744
I have this input field in Rails 3.1:
<%=f.file_field :image, :id => "upload"%>
I want to add this JavaScript before the input field:
onchange="readURL(this);
How can I do this?
Must see in HTML sth like:
<input type="file" onchange="readURL(this); name="post[image]" id="upload">
Upvotes: 2
Views: 3217
Reputation: 8807
Are you using JQuery?
$("#upload").change(function() {
..
});
is one way and
$("#upload").bind("change", function() {
..
});
is another.
Update:
You can write it as:
<%= f.file_field :image, :id => "upload", :onchange => "readURL(this)" %>
This will give you HTML something like:
<input type="file" onchange="readURL(this); return false;" name="post[image]" id="upload">
Upvotes: 2
Reputation: 2332
The preferred way to do this is via unobtrusive JavaScript (essentially adding JavaScript functionality to a page after the DOM is loaded, and making it optional). In rails 3.1 and later, you want to add the JS code in Syed's answer to app/assets/javascript/controller_name.js.coffee
or similiar.
Upvotes: 0