1

I'm using an external JavaScript file to access DOM objects with in my documents. The problem is that jQuery doesn't seem to have access if it's an externally loaded file. My code looks like:

<html>
    <head>
    </head>

    <body>
        <div id="domToChange" someAttribute="hi"></div>
        <script src="officialJqueryLibrary"></script>
        <script src="myJS.js"></script>
    </body>
</html>

///////// myJS.js file

// Returns undefined
$('#domToChange').attr('someAttribute');

The DOM selector doesn't seem to find my div when the JavaScript file is loaded externally. However everything works when I place it inside the HTML document itself. How can I give DOM access to my JavaScript file?

4
  • 1
    some errors in console? Commented Aug 14, 2013 at 22:42
  • In firebug it shows me the script and I've stepped through the script. Is there anything in particular that would keep external js files from accessing dom elements or is it suppose to just work right out of the box? If it's the latter then I probably did something silly, but the code that I posted is greatly simplified. I'm actually executing that code inside an event handler which is why I know the DOM object should be loaded when the script is executed. Commented Aug 14, 2013 at 22:53
  • 1
    Would be nice if everybody that asks would just give a real live code example. jsbin.com/irihew/1/edit took me 1min and all you have to do is add an external source and we can answer you in like 5mins then. Commented Aug 14, 2013 at 22:54
  • @Mr.Student does it work the same way in different browsers ? Commented Aug 14, 2013 at 23:40

4 Answers 4

1

You should wrap all your DOM related code into

$(function() {
    //Code
});

Then the code gets executed when the DOM is fully loaded.

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

Comments

0

Try getting the value for your attribute, like so:

$('#domToChange').attr('someattribute');
//or
$('#domToChange').attr('someAttribute'); // i know you've tried this, but pls check demo

Demo here. On my machine, browser Chrome Version 28.0.1500.95 m, it just works fine.

Comments

0

OK, this is a hit and miss kind of thing (but I believe it be an accurate explanation), but the real explanation for why it is happening lies here.

You need to understand that jQuery is an object that is initialized. So the jQuery object takes time to initiatize. As it says,

is very important to make the distinction between jQuery object and native DOM elements. Native DOM methods and properties are not present on the jQuery object, and vice versa.

So it is not necessary that the jQuery object gets initialized at the same time the DOM gets initialized.

Also, all scripts that are passed have a defer attribute. This is mostly browser dependent.

As it says,

When set, this boolean attribute provides a hint to the user agent that the script is not going to generate any document content (e.g., no "document.write" in javascript) and thus, the user agent can continue parsing and rendering.

And it can sometimes delay the execution of the script. Hence the different result according to different people.

Comments

0

You have to enclose the code like this:

$(function(){
     $('#domToChange').attr('someAttribute');
});

So that the code is executed when the DOM is ready.

1 Comment

No you don't, since it's placed AFTER the element on the page.

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.