I want to perform some task in jQuery when some other function in my JavaScript is called.
To explain the question: Let's say I have function foo(){..} in JavaScript.
While my code is under execution phase, I want to perform some action using jQuery whenever function foo(){..} is called.
Rough Demo:
<!DOCTYPE html>
<title>Demo</title>
<body>
<script>
function foo()
{
alert("Function Called");
}
...Some code....
if(some condition)
foo(); //function Call - I want to execute jQuery event when this line is executed.
else
woo();
</script>
</body>
</html>
Is there any event handler which can achieve this?
After seeing the updated question, the only way to handle this would be to put your jquery function at the same level as function foo() and call it from there as explained in this example.
Related
I'm using click event to execute some code. And after execute the click function code, want to execute another function or code like a callback function. But have struct to call function after the click function.
var $body = jQuery('body');
$body.on('click', '.modal_popup_btn', function() {
var $data_type = jQuery(this).attr('data-popup-link');
jQuery('.modal_wrapper[data-popup="'+ $data_type +'"]').removeClass('modal_closed').addClass('modal_opened');
jQuery('.modal_wrapper[data-popup="'+ $data_type +'"]').find('.modal_container').removeClass('modal_container_closed').addClass('modal_container_opened');
setTimeout(function() {
hiddenBodyScrollWithPadding();
}, 550);
});
Here is the code, need to call 'hiddenBodyScrollWithPadding()' after the click code execute. Here used setTimeout to execute before code. But setTimeout is a bad idea to this situation. Can anyone have another idea to execute this function in a better way?
If your '.modal_wrapper' animating, then add this jQuery Code.. Your Popup modal will listen transition event is ended then check if Modal has .modal_opened class. If transition has been ended and hasClass condition is true then your custom function will be executed.
jQuery('.modal_wrapper').on('transitionend', function(event) {
event.preventDefault();
if(jQuery(this).hasClass('modal_opened')){
hiddenBodyScrollWithPadding();
}
});
I have a code like this:
...
<script>
function myfunction(result) {
$(".dyn").prop("innerHTML",result);
}
</script>
...
<table class="dyn">
</table>
<script>
$.post('some_ajax_page',some_value,myfunction);
$(".myeditable").on(blur,myfunction2);
</script>
...
The POST executes correctly and gets the content of the table, which is displayed correctly, and it is like this:
...
<tr><td class="myeditable">...</td></tr>
...
But the TD doesn't have a listener set for blur, even when the listener intends to be set after the POST has returned and its function executed.
Why doesn't the listener get set?
You need to add the event listener after you insert the html on the callback.
<script>
function myfunction(result)
{
$(".dyn").prop("innerHTML",result);
$(".myeditable").on(blur,myfunction2);
}
</script>
Remember the call back function on $.post() will only execute when the data is received, asynchronously, so the line after $.post() will actually execute before the callback.
can you call a javascript function on event handlers? What I mean is like,
... onClick="dosomething()">
<script type="text/javascript">
function dosomething(){alert("I just did something");}
the code isn't complete but i've tried it this way all nice and neat and it doesn't work, does this mean we have to enter the whole JS code inside the event handler???
You can attach the event handler without specifying it inline. If you know the id of the element its pretty simple.
Javascript
function doSomething(){
alert("I just did something");
}
var elem = document.getElementById("idGoesHere");
elem.onclick = function(){
doSomething();
}
HTML
<div id="idGoesHere">Click Me</div>
JS Fiddle: http://jsfiddle.net/DYqjA/
<script>
document.getElementById("myBtn").onclick=function(){dosomething()};
function dosomething(){alert("I just did something");}
</script>
Crude example using JavaScript
I am trying to call a JavaScript function from an onclick trigger.
HTML section:
<div class="my_radio">
<input type="radio" name="my_radio" value="1" onclick="my_func()"/> first button
</div><!-- end of class my_radio -->
And the JavaScript code
<script type="text/javascript">
$(document).ready(function(){
function my_func(){
alert("this is an alert");
}
});
</script>
It does not work.
But if i keep the JavaScript function out of the $(document).ready() code, it works. Following is the relevant code snippet:
<script type="text/javascript">
$(document).ready(function(){
function my_func111(){
alert("this is an alert");
}
});
function my_func(){
alert("this is an alert");
}
</script>
1) Why does not the first JavaScript code snippet work?
2) How can I get the first JavaScript code snippet working ?
EDIT :
SO FAR AS I KNOW, $(document).ready() is executed when the web page loads completely. So how can I prevent my_func() to be active before or after the complete page-loading if I write my_func() outside $(document).ready()?
It's all about javascript execution contexts and scope.
Everything that you define within a function is know only in this function.
In your first test, the function my_func() can only be used within the ready callback (and in the inner other objects). You can't reference it outside.
In your second example, the function my_func() is global to the document and accessible from anywhere.
I recognize this is maybe a verry simple explanation :-)
If you define your function within a callback, you can only use it within this callback:
$(document).ready(function(){
function something(){
alert('test');
}
//..
something(); // Will work
}
something(); // Won't work
Your first snippet doesn't work, because in in the function my_func111 is defined within the local scope of an anonymous function passed as an argument in your $(document).ready call.
You can fix your code by placing the function definition to the document scope and calling it inside ready function such as:
function my_func(){
alert("this is an alert");
}
$(document).ready(function(){
my_func();
});
I presume by "it does not work", you mean it says "my_func is not defined" or similar?
When you define a function within a function, the inner function is not visible outside of the outer function (unless it is part of the outer function's return statement).
You'll need to learn about closures, a good tutorial on which can be found here.
You'll add a global variable isReady. The $(document).ready callback section will change it to true.
Both your function and the isReady variable must be defined outside the callback of the $(document).ready, so that they can be seen from outside the scope of the callback.
<script type="text/javascript">
var isReady = false; // outside the onReady callback
function myFunc(){ // also outside the onReady callback
if (!isReady) return; // abort if not ready
alert("this is an alert");
}
// the onReady callback
$(function(){ // the newer jquery shorthand for: (document).ready(function(){
isReady = true;
});
</script>
Your HTML code needs no changes. - I changed the names to use JS and HTML conventions, but essentially it's the same as what you originally wrote...
<div class="divMyRadio">
<input type="radio" id="myRadio" value="1" onclick="myFunc()"/> first button
</div><!-- end of class divMyRadio -->
I
As a side note: The newer JQuery uses $(function(){ as shorthand for $(document).ready(function(){ to make things easier for you.
I am trying to create some functionality when a user clicks on an element on the webpage. The callback function executes as soon as the page is executed. It is only supposed to execute when the user clicks on an element.
Here is the code:
<!DOCTYPE html>
<html>
<head>
<title>Javascript Test</title>
<script src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>
$("#clickMe").one('click', printThis("Hello All"));
function printThis(msg) {
console.log(msg);
}
</script>
</head>
<body>
<div id="clickMe">Click me!</div>
</body>
</html>
Thanks!
That isn't actually passing the function, but instead it is evaluating the function and passing the result as the callback parameter (in this case, undefined).
Try this instead
<script>
function printThis(msg) {
console.log(msg);
}
$("#clickMe").one('click', function() {
printThis("Hello All");
});
</script>
Don't invoke the callback. Pass an anonymous callback function that invokes the function you want.
function printThis(msg) {
console.log(msg);
}
$("#clickMe").one('click', function() { printThis("Hello All") });
one method takes a callback as the second parameter. printThis("Hello All") will actually call the method there itself so on click of clickMe nothing will happen as there is no handler attached. Try this
function printThis(msg) {
console.log(msg);
}
$("#clickMe").one('click', function() { printThis("Hello All") });
The answer already posted is right:
$("#clickMe").one('click', function() { printThis("Hello All") });
This is known as a closure: https://developer.mozilla.org/en/JavaScript/Guide/Closures
A closure is a function, often declared as an inline/anonymous block of code, that delays the execution of your function and stores the value of that "Hello All" argument that you want to pass in.
jQuery will store this "function() {...}" code block as an event handler, and then later when the #clickMe element is clicked, that code block will be executed, which in turn will call the printThis function.
You will find yourself using this pattern quite often with jQuery.
Try this ... http://jsfiddle.net/PcVJq/