why I cannot call outside function from $(document).ready - javascript

With below code, click on button doesn't call handleData() function,
if I use anonymous function way (which inside the /* */), it worked.
Can someone explain what the problem is? and how to fix it?
I want to call global function from $(document).ready(...).
<html>
<head>
<script src="js/jquery/jquery-1.7.2.min.js"></script>
<script>
function handleData(data, status) {
$("#p1").text(data);
};
$(document).ready(function() {
$("button").click(function() {
$.get("testAsync?name=value", handleData(data, status));
});
/*
$("button").click(function() {
$.get("testAsync?name=value", function(data, status) {
$("#p1").text(data);
});
});
*/
});
</script>
</head>
<body>
<button>Send an HTTP GET</button>
<p id="p1"></p>
</body>
</html>

The function is being called immediately because you have (arguments) after it.
Since the arguments are in order, just pass handleData alone.

Change the function call from
$.get("testAsync?name=value", handleData(data, status));
to
$.get("testAsync?name=value", handleData);
In the first case you are immediately executing the function.. Just pass in the function pointer and it will work as expected..

Related

How to send paramenter in jquery syntax function?

i want click the button call function with parameter but i don't know how to write syntax
$(document).ready(function () {
$('#ShowMap').on('click', initialize);
})
function initialize(value) {
alert(value);
}
if change code for code down code work but after load page function called
$(document).ready(function () {
$('#ShowMap').on('click', initialize("test"));
})
how to fix the problem ?
This doesn't send the function as a parameter, it immediately executes the function and sends the result (which is undefined) as the parameter:
$('#ShowMap').on('click', initialize("test"));
Instead, wrap it in a function to be the parameter:
$('#ShowMap').on('click', function () { initialize("test"); });
You can pass another function as argument and inside that function call the desired function
$('#ShowMap').on('click', initialize("test")); will not send function as argument it will send the return value of the initialize which is undefined
function initialize(value){
console.log(value);
}
$(document).ready(function () {
$('#ShowMap').on('click',() => initialize("test"));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="ShowMap">Show Map</button>

How can I all a JS function in a browser console if the JS function is included in the HTML?

HTML:
<script type="text/javascript" src="scripts/js/script.js"></script>
JQuery function here:
$(document).ready(function() {
function rnmtn(){
console.log("red");
}
});
How can I call rnmtn(); in the console without it returning undefined - the script is not linked to an html object - I need it to function on its own.
Make your function
function rnmtn(){
console.log("red");
}
and call it in console by typing
rnmtn()
you need to make the function accessible in the window scope,
such like
$(document).ready(function() {
window.rnmtn = function rnmtn(){
console.log("red");
}
});

jQuery onkeyup event: scan not defined

Displays:
Uncatched Reference error: scan not defined.
Here is the code:
<html>
<head>
<script type="text/javascript" src="../js/jquery.js"></script> <script type="text/javascript">
$(document).ready(function() {
function scan() {
$("#result").text("<font style='color: green;'>Cote</font>"); console.log("Function ran");
}
});
</script>
</head>
<body>
<font style="font-size: 84px;">Scan card, please</font>
<p id="result">cotes</p>
<input style="font-size: 36px;" id="input" maxlength=16 autofocus onkeyup="scan();">
</body>
</html>
scan method definition should be outside of the other function's scope. See how you should modify your code:
function scan() {
$("#result").text("<font style='color: green;'>Cote</font>");
console.log("Function ran");
}
$(document).ready(function() {
// remove if not necessary
});
Your scan function is only accessible in the scope of:
function() {
function scan() { ... }
}
You should place it outside this function at the level of the document:
<script type='text/javascript'>
function scan() { ... }
</script>
You should use $(document).ready for initializing the document once its content has been loaded. Over here, you are just declaring the scan function within the scope of this initialization function I've just mentioned but it is not within the scope of the event handler set at the onkeyup event. Therefore, function scan is not found.
If you have any further questions, let me know. If this solves the issue, don't forget to vote up! :)

Give selector.method as parameter, call it

I've got a function $.doSomething() which does some stuff and then should call other functions in the jQuery-form of $('selector').function(). I do not want $.doSomething() to know about what it's calling, that should be decided by the user.
I created a small example on JSBin:
http://jsbin.com/umonub/11/edit
Is it possible to store the call to $('#foo').doStuff(); in a function-object and give it as parameter to a function which then calls it?
Here's the sample-code (in case somebody can't access JSBin or it gets deleted there):
script.js
$(document).ready( function() {
var callback = $('#foo').doStuff();
$.doSomething(callback);
});
$.extend({
doSomething: function(callback) {
//do some stuff
//call the callback
}
});
$.fn.doStuff = function() {
$(this).text("Stuff Done");
};
index.html
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<p id="foo"></p>
</body>
</html>
Sure, just put $('#foo').doStuff() inside a function:
var callback = function() {
$('#foo').doStuff();
};

how to call function in jquery from javascript

<script type="text/javascript" src="jscripts/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert("funciton");
$(function(){
$.fn.gotof(){
alert("I am calling form jquery");
}
});
});
</script>
<input type="button" onclick="dofunc();">
<script type="text/javascript">
function dofunc(){
gotof();
}
</script>
how do i call gotof() that is present in jquery
and below is the code written over jsfiddle
There are a few errors in your code. Fixed it should look like this:
$.fn.gotof = function() { // has to be defined as a function, does not need to be inside a nested document ready function
alert("I am calling form jquery");
};
$(document).ready(function() {
alert("function is ready to use now");
});
function dofunc() {
$.fn.gotof(); // can call it using $.fn.gotof(), but it should really be called properly via a selector $('div').gotof();
}
http://jsfiddle.net/pSJL4/8/
Check out http://docs.jquery.com/Plugins/Authoring - this should answer your questior. You also are not defining your function correctly; instead of
$.fn.gotof(){
alert("I am calling form jquery");
}
you need
$.fn.gotof = function(){
alert("I am calling from jquery");
};

Categories

Resources