-1

Is it possible to create a function in the onload event of an image? Is there a method that makes this possible?

<input type='text' name='data' onblur='myFunction(this.value)'>
<img src='img/none.gif' onload="function myFunction(text){alert(text)}">

the image and field will be loaded by an ajax function.

Spiderpoison
  • 95
  • 2
  • 9
  • duplicate of https://stackoverflow.com/questions/280049/how-to-create-a-javascript-callback-for-knowing-when-an-image-is-loaded – Ankit Kumar Dec 18 '18 at 12:14
  • Possible duplicate of [How to create a JavaScript callback for knowing when an image is loaded?](https://stackoverflow.com/questions/280049/how-to-create-a-javascript-callback-for-knowing-when-an-image-is-loaded) – Pardeep Dhingra Dec 18 '18 at 12:15
  • @PardeepDhingra — Not a duplicate of that. The code already runs a callback when the image is loaded. The question is about creating a function from within that callback. – Quentin Dec 18 '18 at 12:18
  • @AnkitKumar — Not a duplicate of that. The code already runs a callback when the image is loaded. The question is about creating a function from within that callback. – Quentin Dec 18 '18 at 12:19
  • @Quentin has answered your question. May i know the reason why you want to do so. – Ankit Kumar Dec 18 '18 at 12:21
  • The context is different, the event is inside the IMG tag – Spiderpoison Dec 18 '18 at 12:21
  • I am developing a system for working with access control boards, the boards can be of different models and the devices for reading card, biometrics or control work differently on each board.To unify my control of registered devices I need a single structure in my database Because the fields are different in each situation and are loaded with information from a database, passed through an ajax function to form the fields.I need javascript functions differentiated in each situation. So to do this I make each set of fields that is loaded create their own script to interact with the sensors. – Spiderpoison Dec 18 '18 at 13:08

1 Answers1

2

Your code is using a function declaration, so you are defining the myFunction variable only within the scope of the onload function (so it isn't in scope when you try to call it from the onblur function).

So you can, you just need to explicitly assign it to be a global variable.

<input type='text' name='data' onblur='myFunction(this.value)'>
<img src='https://static.jsbin.com/images/dave.min.svg' onload="window.myFunction = function myFunction(text){alert(text)}">

… I can't think of any good use case for this though.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Tks Quentin, perfect solution. – Spiderpoison Dec 18 '18 at 12:24
  • It is a complex system where the functions for sending commands to the control cards require different fields for each situation. The main control system reads only 1 field, so the function created in the image will aggregate the values of the available fields to pass the value in a single location. All data is created with ajax. – Spiderpoison Dec 18 '18 at 12:30