admin管理员组文章数量:1399737
I am facing problem in calling a function demo1()
directly on button call.
Please See Code
var FormImageCrop = function (){
var demo1 = function() {
var jcrop_api;
$('#demo1').Jcrop({
onChange: showCoords,
aspectRatio: 4 / 3
},function(){
jcrop_api = this;
jcrop_api.setSelect([0,0,3200,2400]);
});
$('#coords1').on('change','input',function(e){
var x1 = $('#x11').val(),
x2 = $('#x12').val(),
y1 = $('#y11').val(),
y2 = $('#y12').val();
jcrop_api.setSelect([x1,y1,x2,y2]);
});
function showCoords(c)
{
$('#x11').val(c.x);
$('#y11').val(c.y);
$('#x12').val(c.x2);
$('#y12').val(c.y2);
$('#w1').val(c.w);
$('#h1').val(c.h);
};
}
return {
init: function () {
if (!jQuery().Jcrop) {;
return;
}
demo1();
}
};
}();
i want to call this function on button click
like <button onclick="demo1()">Click it</button>
How can i call this function???
I am facing problem in calling a function demo1()
directly on button call.
Please See Code
var FormImageCrop = function (){
var demo1 = function() {
var jcrop_api;
$('#demo1').Jcrop({
onChange: showCoords,
aspectRatio: 4 / 3
},function(){
jcrop_api = this;
jcrop_api.setSelect([0,0,3200,2400]);
});
$('#coords1').on('change','input',function(e){
var x1 = $('#x11').val(),
x2 = $('#x12').val(),
y1 = $('#y11').val(),
y2 = $('#y12').val();
jcrop_api.setSelect([x1,y1,x2,y2]);
});
function showCoords(c)
{
$('#x11').val(c.x);
$('#y11').val(c.y);
$('#x12').val(c.x2);
$('#y12').val(c.y2);
$('#w1').val(c.w);
$('#h1').val(c.h);
};
}
return {
init: function () {
if (!jQuery().Jcrop) {;
return;
}
demo1();
}
};
}();
i want to call this function on button click
like <button onclick="demo1()">Click it</button>
How can i call this function???
Share Improve this question asked Feb 7, 2015 at 10:46 M Uzair QadeerM Uzair Qadeer 4921 gold badge8 silver badges20 bronze badges 2- 1 Why do you need to define the function within another function? – wnbates Commented Feb 7, 2015 at 10:51
- Define outside and call it twice - once from the function and once from the button. – sideroxylon Commented Feb 7, 2015 at 10:54
3 Answers
Reset to default 4var myObject = {
firstName:"John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
myObject.fullName(); // Will return "John Doe"
That is all. You can read this whole tutorial from here http://www.w3schools./js/js_function_invocation.asp
Or you can try this
var my_foo= function foo() {
// do something
}
myfoo();
or
function bar() {
// do something
}
bar();
The only way to call function within the HTML is if the function itself is exposed on the global window object. I wouldn't necessarily do it like this but if you still want to call the function on the onlick
property of the button you could do it like this:
var FormImageCrop = function (){
window.demo1 = function() {
...
...
}
return {
init: function () {
...
...
window.demo1();
}
};
}();
As i said, this is no ideal but it will work. A better alternative is to add an event listener to the button.
Hope it helps.
You can also like that
var FormImageCrop = function (){
window.demo1 = function(val) {
alert(val);
}
return {
init: function () {
window.demo1();
}
};
}();
And button Link this
<button onclick="demo1('abc')">Click It</button>
本文标签: javascriptCall a function within function in JqueryStack Overflow
版权声明:本文标题:javascript - Call a function within function in Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744187014a2594338.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论