0

I have a script that I am using to share images. After the script (at the bottom) in the same js file I have the following:

$(document).ready(function() {
    $(".shared").after('<div class="share-button"></div>');
});

new Share(".share-button", {});

For some reason the script does not work. But when I have the page up and enter the above script in, it loads and the div shows up.

Am I missing something here?

5
  • What is 'new Share'?? Commented Aug 15, 2014 at 21:43
  • Presuming the "new Share" part instantiates something onto the element you're creating above that line, the creation is happening once the DOM has loaded and the instantiating isn't - therefore when you run new Share, the element doesn't exist yet. When you put it into your console manually, it will work because the element does then exist. Put new Share() into your document ready scope. Commented Aug 15, 2014 at 21:44
  • 1
    what's the error that the browser console gives you? Commented Aug 15, 2014 at 21:45
  • Its calling the actual share button plugin. Here is a link to see the complete code link Commented Aug 15, 2014 at 21:45
  • Im not getting an error in the browser, its just not loading. Normally, I would just add this to the footer surrounded by script tags but I have to everything in one js file. Does that matter? Commented Aug 15, 2014 at 21:49

1 Answer 1

1

Looks like you're trying to instantiate a Share object that depends on the "share-button" elements existing, but those elements don't exist because they're added in $(document).ready(). Try this:

$(document).ready(function() {
    $(".shared").after('<div class="share-button"></div>');
    new Share(".share-button", {});
});

Note that the "new Share()" is inside the ready handler, after the "share-button" has been added to the page.

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

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.