Dynamic Add method of HTML element

Source: Internet
Author: User

Today do dongdong time to use JS to create HTML elements and then dynamically add methods to the element, walked a lot of detours. Finally, the solution to release, so that everyone after less detours.

JS file

function Checknull (ID)

{

DoSomething

}

var checknewnull= function (ID)

{

return function () {

Checknull (ID);

}

}

Adding methods to HTML elements

Create a TEXTAREA

Std2area=document.createelement ("textarea");

Setting properties

Std2area.setattribute ("id", "member_task" +position);

Std2area.setattribute ("name", "Member_task" +position);

Add method

Std2area.attachevent ("onblur", Checknewnull ("Member_task" +position));

If the Add method is written as: Std2area.attachevent ("onblur", Checknull ("Member_task" +position));

or written std2area.attachevent ("onblur", checknull);

Then there will be an error. The reason for this is to assign the return value of Checknull ("Member_task" +position) to onblur

Instead of assigning the function checknull to the element. So the right thing to do is to return checknull with a function, as shown above.

Other ways to write the final Add method:

Std2area.onblur=checknull;

Std2area.setattribute ("onblur", checknull);

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.