falohotels.blogg.se

Get element by class javascript
Get element by class javascript











get element by class javascript

Short and simple: Because the elements you are looking for do not exist in the document (yet).įor the remainder of this answer I will use getElementById for examples, but the same applies to getElementsByTagName, querySelector, and any other DOM method that selects elements. For efficiency, select the nearest reliable ancestor of the target element rather than attaching it to the document. Typically, this pattern is reserved for elements that don't exist at load time or to avoid attaching a large number of duplicate handlers. All we have to do is check the event to see whether it was raised by the desired element and, if so, run our code. even from descendants added after the handler was attached. That allows us to attach a handler to an existing element and sample events as they bubble up from its descendants.

get element by class javascript

When an element raises an event (provided that it's a bubbling event and nothing stops its propagation), each parent in that element's ancestry, all the way up to window, receives the event as well. Delegate the event handling to an ancestor of the target element.













Get element by class javascript