0

I'm trying to make a jQuery (or even a Javascript) function with a function using $("script").append(, but once it is made the "page" or "doc" has already loaded and so doesn't "see" it. (the function wasn't there when it loaded so to the page it "doesn't exist", even though it IS created and viewable). Is there ANY way to reload the <script> or "re-document.ready" the jQuery so the page knows that it NOW exists.

I know it's the document.ready loading process because if I hard code the functions it works just fine (because it exists before the page loads). Somebody HAS to know how to do this or how to work around it. How do you use $("script").append(?

This is an example of what I'm trying to do.

function makeFunction(array){
$("script").append("$(document).ready(function({$('#"+array+"').on('click', function(){$('#"+array+"').hide();});});");
}

This function is called within another function that runs through a loop. The looping function is being called directly after the <script> tag. That way (to my understanding) it loads with the document.ready. The problem is that then the newly created functions are never made "ready" by the initial document.ready loading (because again, they didn't existed yet).

So I'm asking if there is a way to work around this so I can have a singular function write my many functions for me.

If I missing any vital information please be specific as to what "context" is needed.

CamdyCorn
  • 121
  • 1
  • 3
  • 9
  • Is there [any reason](http://meta.stackexchange.com/q/66377/147640) to try to generate literal javascript code in the document instead of creating functions as usual and assigning them to variables? – GSerg Jan 22 '17 at 07:39
  • Welcome to SO. Please visit the [help] and see how and what to ask. HINT: Post effort and code. Also in this case please tell use WHAT you are trying to do so you do not fall into the trap of an [XY-problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – mplungjan Jan 22 '17 at 07:40

2 Answers2

0

No one[citation needed] has successfully used $("script").append() because, even if that worked as you think it should, there would be no need to do it.

Functions are first class citizens in Javascript. To create a new function at runtime you do not need to amend the source code of the page.

Then, the jQuery's ready handler runs the passed function when the document becomes ready, and if the document is already ready when you register another function, the function will be called straight away.

So your unnecessarily complicated and non working code should be replaced with:

function makeFunction(array) {
    $(function() {
        $("#"+array).on("click", function(){ $(this).hide(); });
    });
}

The $(this) improvement is not necessary to answer your question, and you can continue to use $("#"+array) instead even though there is no need to.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
0

I found out that using php was a much better method of doing this. Limiting myself to just javascript was silly, but I didn't know better then. To answer my own question it would be better to loop through the creation of the functions in a php document so when the page is "made" all the elements are created before hand and thus rendered on the page.

CamdyCorn
  • 121
  • 1
  • 3
  • 9