1

I know how trigger a function when an element loads in with jQuery but can't seem to figure it out in pure Javascript. I would like to do this is JS.

So I know how to fire a function in jQuery when an element loads but I want to do it in Javascript. I would like to do this is pure Javascript.

$('img').load(
      function(){
        //do stuff
})
6
  • An input doesn't generally load anything, and has no onload handler, nor does it have children. Commented Sep 9, 2017 at 20:32
  • 1
    "input loads" ... loads from where? Commented Sep 9, 2017 at 20:33
  • 1
    If you can access the element with $('input'), then it is already "loaded", i.e. ready. Commented Sep 9, 2017 at 20:33
  • The input just loads in from js creating an element. Commented Sep 9, 2017 at 20:41
  • Creating inputs is synchronous, it doesn't really load either? Commented Sep 9, 2017 at 20:44

1 Answer 1

2

From the MDN documentation of the load event:

The load event is fired when a resource and its dependent resources have finished loading.

As mentioned in the comments, an input element has no dependent content to load, hence a load event for it will never trigger.

It's important to understand that all elements do need to be parsed into the Document's Object Model, but that is a process that must complete before you can programmatically interact with the element in the first place.

Once the client has finished parsing all the elements in the DOM, the client will fire off its "DOMContentLoaded" event (equivalent to the JQuery $(document).ready event shown in the W3 Schools example you linked to). It is at this point that it is safe to search through the DOM and locate elements to be worked with.

From there, different elements can/will have different events that can occur on them during their lifetime. An input element will respond to user input and interactions, so it has several different events to tie into them (input, change, keydown, keyup, keypress, mouseup, mousedown, focus, blur, etc.).

For a load event, you need to work with elements that contain content that needs to be loaded into them after the element itself has been created. Examples would be window, img, iframe, link. These are all elements that need external content for them to be of any use to anyone.

So, take an element like an img which needs to load an external resource and the pure JavaScript would use the native DOM API way to register event callbacks, which is with the element.addEventListener() method.:

 // Get a reference to the DOM element.
 // Make sure this is done AFTER the DOM has finished loading:
 var img = document.getElementById("someImgageElementsID");

 // Register a function to the image's load event handlers:
 img.addEventListener("load", doLoad);

 // Declare the callback function:
 function doLoad(){
   console.log("image loaded");
 }

More on element events.

Sign up to request clarification or add additional context in comments.

1 Comment

k input was just an element I came up with. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.