# \[SOLVED\] Generated onclick call not finding function

**URL:** <https://discourse.aurelia.io/t/solved-generated-onclick-call-not-finding-function/3898>\
**Category:** Help Requests\
**Created:** [November 24, 2020, 5:51pm UTC](https://discourse.aurelia.io/t/solved-generated-onclick-call-not-finding-function/3898 "2020-11-24T17:51:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhysshadow](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/rhysshadow/32/1959_2.png) [@rhysshadow](https://discourse.aurelia.io/u/rhysshadow)\
**Post date:** [November 24, 2020, 5:51pm UTC](https://discourse.aurelia.io/t/solved-generated-onclick-call-not-finding-function/3898/1 "2020-11-24T17:51:16Z")

</div>

I have some text that is being generated from a script that I am calling thru httpClient.fetch.

It returns some html code that I then insert into the innerHTML attribute in the viewModel like so:

`<p innerHTML.bind="note.htmlComments"></p>`

This is the code generating the text:

```auto
my $txt = "<a name=\"$item_no\">
</a>
<div id=\"short_$item_no\"$short_style>$short_text<br>\n
<span class=\"span spanLink\" onclick=\"expand_div($item_no)\">+ Expand $label</span>
</div>
<div id=\"long_$item_no\" $long_style>$shrink_text<br>\n$long_text<br>\n$shrink_text</div>";

```

This is the html code from the inspect element:

```auto
<span class="span spanLink" onclick="expand_div(c64293822)">+ Expand this note</span>

```

It is displaying properly in the html when I load the page, and I can tell that it’s loaded properly because I have classes ‘span and spanLink’ and they are doing what they should.

My problem is when I try to click on `Expand this note`, the function `expand_div` is not found.

I have tried the following:

In the `activate` function, I have tried this:

```auto
let scriptElement = document.createElement('script');
    scriptElement.src = '/expand_collapse.js'; (have also tried './expand_collapse.js')
    this.scriptElementInHead = document.querySelector('head').appendChild(scriptElement);

```

This produces a `GET` error because it cannot find the file.

Also this:

```auto
    <script type="text/javascript" src="./expand_collapse.js"></script>

```

This produces no errors, but the function is never found.

Also, I have tried including the function directly into my customer.js file as follows:

```auto
expand_div(id)
  {
      var form = document.forms['notes_form'];
  var short = 'short_' + id;
  var long = 'long_' + id;
  
  console.log('expand_div', d.short);
  
  }

```

Contents of expand\_collapse.js:

```auto
export function expand_div(id)

{

    var form = document.forms['notes_form'];

var short = 'short_' + id;

var long = 'long_' + id;

console.log('expand_div', d.short);

}

```

I need to figure out how to make this work, because the script is generating long and short versions of the notes that I want to dynamically display based on a click from the user.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [November 30, 2020, 7:30am UTC](https://discourse.aurelia.io/t/solved-generated-onclick-call-not-finding-function/3898/2 "2020-11-30T07:30:59Z")

</div>

```auto
<div onclick="call_some_function(someArguments)"></div>

```

is a native html attribute, and an old way to “define” a click listener on an element. this requires the method being called to be a global function. You will need to do something like this:

```auto
window.call_some_function = function(myArguments) {
  // ...
}

```

If you want Aurelia to work with the click that way, it should be:

```auto
<div click.delegate="...">
or
<div click.trigger="...">

```

* * *

It’s probably overridable, and maybe easy to do: tell Aurelia that `onclick` is equal to `click.trigger`. But I think unless we have good reason to do that, it’s best avoid it.

---

<div class="post-metadata">

**Author:** ![rhysshadow](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/rhysshadow/32/1959_2.png) [@rhysshadow](https://discourse.aurelia.io/u/rhysshadow)\
**Post date:** [November 30, 2020, 11:05pm UTC](https://discourse.aurelia.io/t/solved-generated-onclick-call-not-finding-function/3898/3 "2020-11-30T23:05:52Z")

</div>

Thank you for adding this.

Since it was a short function, I went ahead and added the code straight to the `onclick` function.

It’s working fine, and I don’t anticipate needing to do this again, because I am trying with all my might to make sure I generate the code for Aurelia thru javascript, and not externally.
