how to make jquery work with ajax? [duplicate] - javascript

This question already has answers here:
jQuery ajax load() java script not executing?
(8 answers)
Closed 8 years ago.
Have the below code which loads a php page into a div, once this content is added the jquery on that php page doesnt seem to work. It only works if you open it directly (without ajax)
any ideas?
AJAX CODE
<script>
$(document).ready (function() {
$('#NavButton1').click(function(event) {
$('#content').empty();
$('#content').load('placeholder.php');
})
})
</script>
Jquery code on PHP page
<script>
$(document).ready(function() {
// call the tablesorter plugin
$('#myTable').tablesorter({
// sort on the first column and third column, order asc
sortList: [[0,0],[2,0]]
});
});
</script>

document.ready is not triggered when you load your PHP.
But the right way to have a script run after you load external content would be to use a callback function:
$('#content').load('placeholder.php', function() {
// call the tablesorter plugin
$('#myTable').tablesorter({
// sort on the first column and third column, order asc
sortList: [[0,0],[2,0]]
});
});

Related

How do I continue executing my .js code after programmatically adding jQuery to the DOM? [duplicate]

This question already has answers here:
How to include jQuery dynamically in any website using pure javascript
(9 answers)
Closed 25 days ago.
How do I continue executing my .js code after programmatically adding jQuery to the DOM?
The 3rd answer in this post shows how to add jquery to the dom but how should you continue executing further code?
For example you can't just add code underneath the self invoked function because jQuery hasn't yet loaded. How do I continue writing my .js code so that it executes?
$(window).on('load', function() {
// jQuery hasn't yet loaded so can't use this yet.
});
Execute the code that requires jQuery in the script's load event listener.
var jQueryScript = document.createElement('script');
jQueryScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js');
jQueryScript.addEventListener("load", function() {
$(document).ready(function() {
...
});
});
document.head.appendChild(jQueryScript);

Add class to dynamically added element [duplicate]

This question already has answers here:
jquery .load( ) and trigger function AFTER new content loads? just like JavaScript onload event
(3 answers)
Closed 8 years ago.
I have a shared left menu in a separate file from my pages so that I can update it in one place.
I am loading it onto the page in a .js file like so:
$(function () {
$("#left-nav").load("templates/left-nav.html", function() {
...
});
...
}
I would like to be able to set the nav-list item for the page to be selected. Something like this:
$("#calendar").addClass("selected");
...where #calendar is the ID of a list item in my left-nav.html template file.
Everything I've read says that I have to call addClass as a callback after I load in the file, but the following doesn't seem to work:
$(function () {
$("#left-nav").load("templates/left-nav.html", function() {
$("#calendar").addClass("selected");
});
...
}
Question: Is it possible to add classes to dynamically added content on a page, and if so, how should I do it? Thanks!
The code is correct as written, adduming that left-nav.html does contain an element with the ID of calendar.
The completed event is fired after the HTML is added to the DOM according to the jQuery documentation.

Run Javascript code on Page Load [duplicate]

This question already has answers here:
Onclick() function on working
(4 answers)
Closed 9 years ago.
I am using the jquery tool bootstrap wizard and there is a function within it to start the wizard that I want to load up automatically when I go to a page.
Here is my code:
$('#open-wizard').click(function (e) {
e.preventDefault();
wizard.show();
});
I need to have this code run whenever I load the page. I know for functions you could just add, not sure how I do this with my code, I tried to put it in a function and didn't work.
window.onload = load();
function load() {
//javascript function here
}
when using window.onload you do not need the perenthesis. change load() to load..
function load() {
//javascript function here
}
window.onload = load;
Yes,and i suggest u put the js import tags at the bottom of the page file.
eg:
<scrpit ...
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
</body>
</html>
document.ready (jQuery)
$(document).ready(function()
{
// executes when HTML-Document is loaded and DOM is ready
alert("(document).ready was called - document is ready!");
});
document.ready will execute right after the HTML document is loaded property, and the DOM is ready
window.load (Built-in JavaScript)
$(window).load(function()
{
// executes when complete page is fully loaded, including all frames, objects and images
alert("(window).load was called - window is loaded!");
});
The window.load however will wait for the page to be fully loaded, this includes inner frames, images etc.
all you need is $(document).ready();
$(document).ready(function () {
wizard.show();
});

how to call a javascript method soon after page load [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript that executes after page load
how to call a javascript method as soon as page is loaded.
i have a java script which needs to be called soon after the jsp page is loaded, how to achieve this in javascript.
could any one of you help me pelase.
Regards
You can write a code snippet something like this :-
window.onload(function(){
//Your JavaScript here
});
And if using JQuery then
document.ready(function(){
//Your JavaScript Here
});
Or you can have all your JS after all the HTML.
you can even use a function called :--
document.onload(function(){
//Your code Here
});
Last but not the least you could even try out this
<body onload="YourJSMethod();">
<!-- Some html content -->
</body>
you can try
<body onload="someMethod();">
<!-- Some html content -->
</body>
This will call your method as soon as body tag of your page is loaded.
Alternatively you can make use of jquery's document ready function.
$(document).ready(function() {
// your code
});

jQuery "$(document).ready(function () {" equivalent in Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
$(document).ready equivalent without jQuery
I want to wait until an ASP.NET datagrid is resized correctly before showing a popup message. This works great:
$(document).ready(function () { showpopup(); });
But I need to acheive it without jQuery. I've tried many ways:
$(window).bind("load", function() {showpopup();}
$(function() { showPopup();}
but this doesn't work.
My preferred method of doing such things is to start my script with:
var loadScripts = [],
loadScript = function(callback) {loadScripts.push(callback);
Then, the very last thing on the page before </body> is:
<script type="text/javascript">(function() {var x; while(x=loadScripts.shift()) x();})();</script>
Then, whenever there's something I want to defer until the DOM has loaded, I simply enclose it in:
loadScript(function() {
// do stuff here
});

Categories