Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>And this is where you learn about scoping. You've defined <code>myFunction</code> inside the success handler, so it can only be called from inside the success handler. However, your <code>&lt;a href="..."&gt;</code> lives in <code>window</code> scope (because in the success handler it's just a meaningless text string, and the <code>append</code> call turns it into a a real HTML element that has access only to the <code>window</code> scope), which doesn't know about your <code>myFunction</code> function, so now you're trying to call a function that does not exist.</p> <p>In JavaScript, everything is scoped (i.e., "visible to things in") to the function they are in. Variables declared with <code>var ...</code> inside a function X don't exist outside that function X, and functions defined inside a function X don't exist outside that function X.</p> <p>In this case, you want to do this, instead (noting that <code>value</code> is already a string, using <code>var string = String(value)</code> makes no sense):</p> <pre><code>var newElement = $("&lt;a href='#'&gt;" + ... + "&lt;/a&gt;...: " + ... + "&lt;/br&gt;"); newElement.click(myFunction); $('#data_box').append(newElement); </code></pre> <p>Except you don't, because you're using an <code>&lt;a href="#"&gt;</code>, and I'm 100% sure not trying to set up a link to the top of your page: <em>don't use <code>&lt;a&gt;</code> when you don't mean it</em>.</p> <p>What you <strong>really</strong> want, is this:</p> <pre><code>var newElement = $("&lt;div&gt;&lt;span&gt;" + ... + "&lt;/span&gt;...: " + ... + "&lt;/div&gt;"); newElement.find("span").click(myFunction); $('#data_box').append(newElement); </code></pre> <p>And yes, that doesn't have the <code>&lt;br&gt;</code> anymore either. Don't use break codes, use CSS.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload