Function is not defined in onclick from html

I am trying to use a function call within a repeat.for so that I can use variables that come through inside the loop, but it’s not working.

Error:
Uncaught ReferenceError: select_by_age is not defined at HTMLImageElement.onclick

I had been using a click.call to call the function, but my variables were not defined if I did that, so I changed it to a onclick=.

Html code:

                            <table cellpadding="2" cellspacing="4" width="400"
                                border="0" class="customerinfobg">
                                <tr repeat.for="aging of aging_table">
                                    <td class="customerinfocellfg" align="right"
                                        valign="center">
                                    ${aging.label}
                                    </td>
                                    <td class="customerinfocellfg" align="right"
                                        valign="center">
                                        ${aging.dollar_amount}                                    
                                    </td>
                                    <td class="customerinfocellfg" align="center"
                                        valign="center">
                                        ${aging.item_count}
                                    </td>
                                    <td bgcolor="#ffffff" valign="center">
                                        <img src="graphs/leftbar.gif" width="4"
                                            height="20" 
                                            onclick="select_by_age($aging.from_age, $aging.to_age);">
                                        <img src="./graphs/redbar.gif" width="${aging.redbar}"
                                            height="20" 
                                            onclick="select_by_age($aging.from_age , $aging.to_age);">
                                        <img src="graphs/rightbar.gif" width="4" height="20"
                                            onclick="select_by_age($aging.from_age, $aging.to_age);">
                                    </td>
                                </tr>
                            
                            </table>

Js code:

select_by_age(min_age, max_age)
      {
        console.log('min_age', min_age);
        console.log('max_age', max_age);
      }

I can’t quite figure out what I should be trying to do.

1 Like

click.delegate=“select_by_age(aging.from_age, aging.to_age)”

… should solve your problem?

4 Likes

Yes, click.delegate would be my preference as well.

@rhysshadow . . . In previous posts (like defaultDate for native Datepicker not working), you also seem to use something like onclick.call="...". I am not familiar with that construct - I haven’t investigated it yet so I don’t know about any drawbacks - but if it used to work for you, perhaps you could use that here too.

1 Like

@dnkm - That worked perfectly! Thanks.

1 Like

Nice one. Keep going, keep learning. I think it’s an exceptional framework, and my go to for pretty much everything now.

4 Likes