0

I have the following code which loads jQuery into the page dynamically after page load and then attempts to run some jQuery of its own afterwards. The first console log of the page title works. The issue comes when it cant find the class "special-div" later on in the page and replace it with the appropriate text. Any thoughts?

//Load jQuery library using plain JavaScript
(function(){
    var newscript = document.createElement('script');
    newscript.type = 'text/javascript';
    newscript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(newscript);

// Poll for jQuery to come into existance
var checkReady = function(callback) {
    if (window.jQuery) {
        callback(jQuery);
    }
    else {
        window.setTimeout(function() { checkReady(callback); }, 100);
    }
};

// Start polling...
checkReady(function($) {
    console.log( 'jQuery is loaded on: ' + $('title').text() );
    $( '.special-div' ).each(function( index ) {
        console.log( index + ": " + $( this ).text() );
        $( this ).replaceWith( "Say something here " + $( this ).attr( "id" ) + ' ' + $( this ).attr( "title" ) );
        });
    });
})();

The HTML looks like this:

<div id="something" class="special-div" title="else">&nbsp;</div>

The wacky CMS that I am working on only allows for me to paste in one external javascript file so i have to load in jQuery and all other scripts i need through that one file.

Edit:

so i ran a few additional tests and tried this:

console.log( 'jQuery is loaded on: ' + $( '.special-div' ).attr( "id" ) );

the response i am getting is:

jQuery is loaded on: undefined
4
  • 1
    I tried it with jsfiddle and it works jsfiddle.net/L072eyax Commented Mar 30, 2015 at 14:35
  • 1
    The code works as-is, but the div is deleted by the call to replaceWith. Are you trying to refer to .special-div after that? Commented Mar 30, 2015 at 14:45
  • nope. in th page i am working on the code does nothing and the console log for the each index never happens. Commented Mar 30, 2015 at 14:51
  • Check this: stackoverflow.com/questions/17451616/… Commented Mar 30, 2015 at 14:54

3 Answers 3

0

If you want to return content of div in second console.log, use $( this ).html() instead of $( this ).text()

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to replace text for each $( '.special-div' ) with the content of their attributes, you have to do:

$( this ).replaceWith( "Say something here " + $( this ).attr( "id" ) + ' ' + $( this ).attr( "title" ) );

instead of

$( '.special-div' ).replaceWith( "Say something here " + $( this ).attr( "id" ) + ' ' + $( this ).attr( "title" ) );

otherwise you get the same replacement for all occurrences.

2 Comments

Thanks i have made that change but still nothing. I edited the original with some more information.
Using the code as-is I get the right output, have you any other JS error in browser console (Firebug, Chrome Inspector or whatever)?
0

I tried it and it works.
But if you are putting it in <head> and another jQuery is loaded before it, DOM searching will run before the DOM getting ready so that it can not find the DOM.
(case of no window.setTimeout(function() { checkReady(callback); }, 100);)
(another CMS plugin might load jQuery)

So it could be better to run the script on kind of window.onload timings.
Or putting it on the end of <body> may also work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.