admin管理员组文章数量:1415460
I am using JavaScript in my application in a function name cropper like that
function cropper(){
var selectedImg = $('#image_file')[0].files[0];
}
I am calling it using file element with ID image_file from my html like this
<input type="file" id="image_file" name="picture1" onchange="cropper()"/><br>
All I want to change the above function like
function cropper(variable){
var selectedImg = variable[0].files[0];
}
so that I can assign different ID for different file element. Could you please suggest me how can I achieve the above functionality.
Edit:
I have 4 file attachment button in my website and I wants to use different ID for that so it would be like that.
<input type="file" id="picture1" name="picture1" onchange="cropper(picture1)"/><br>
<input type="file" id="picture2" name="picture2" onchange="cropper(picture2)"/><br>
<input type="file" id="picture3" name="picture3" onchange="cropper(picture3)"/><br>
<input type="file" id="picture4" name="picture4" onchange="cropper(picture4)"/><br>
I am using JavaScript in my application in a function name cropper like that
function cropper(){
var selectedImg = $('#image_file')[0].files[0];
}
I am calling it using file element with ID image_file from my html like this
<input type="file" id="image_file" name="picture1" onchange="cropper()"/><br>
All I want to change the above function like
function cropper(variable){
var selectedImg = variable[0].files[0];
}
so that I can assign different ID for different file element. Could you please suggest me how can I achieve the above functionality.
Edit:
I have 4 file attachment button in my website and I wants to use different ID for that so it would be like that.
<input type="file" id="picture1" name="picture1" onchange="cropper(picture1)"/><br>
<input type="file" id="picture2" name="picture2" onchange="cropper(picture2)"/><br>
<input type="file" id="picture3" name="picture3" onchange="cropper(picture3)"/><br>
<input type="file" id="picture4" name="picture4" onchange="cropper(picture4)"/><br>
Share
Improve this question
edited Jun 5, 2013 at 1:40
Ashish
asked Jun 5, 2013 at 0:56
AshishAshish
14.7k22 gold badges87 silver badges137 bronze badges
2 Answers
Reset to default 3You can pass the event object to the handler
<input type="file" id="image_file" name="picture1"
onchange="cropper(event)"/><br>
Then use the event object in the method
function cropper(event){
var selectedImg = event.target.files ? event.target.files[0]
: $('#image_file')[0].files[0];
}
//variable = id of an element.
function cropper(variable){
var selectedImg = $('#'+variable)[0].files[0];
}
本文标签: jqueryHow to pass file element in JavaScript functionStack Overflow
版权声明:本文标题:jquery - How to pass file element in JavaScript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745151130a2644925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论