How to bind events with JS for dynamically generated elements

Source: Internet
Author: User
Today, when developing a project, you need to bind a click event to a dynamically generated element of JS, but before the page loads, the element does not exist, it cannot be fetched, and then an event is added to it. Review the jquery API discovery to use the On () event (note: The new version of jquery has removed live (), is now on ()), binds events to dynamically produced elements, and then triggers the event execution. For example, one of the following:

Let's say we're going to add the span binding click event to the div dynamically to produce the following result

x//The procedure isas follows:("#choose_result"). On ("click", "Span", function () {
Do something here
});

To actively trigger a span's Click event
(". Close"). Trigger ("click");

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to bind events with JS for dynamically generated elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.