Set attribute node (SetAttribute ())

Source: Internet
Author: User

SetAttribute (): Method adds a new property value to the given element node or changes its existing property value:

Element.setattribute (Attriibutename,attributevalue);

The name and value of the property must be passed to this method as a string, and if the property already exists, its value will be flushed and if it does not exist, the SetAttribute () method can only be used on the attribute node.

In the following example, the SetAttribute () method adds a value of the title property of "This is important" to the ID attribute value that is on the fineprint element:

var message = document.getElementById ("FinePrint"); Message.setattribute ("title", "This is Importanr ");

Regardless of whether the element "FinePrint" has previously had a title attribute, he will now have a value of the title property of this is important.

Even if an element has not been inserted into the document tree, the SetAttribute () method can also be set on its attribute node, and if a new element is created with the createelement () method, you can set it to its properties before adding it to the document tree:

var para = document.createelement ("P");p Ara.setattribute ("id", "fineprint"); var container = document.getElementById ("content"); Container.appendchild (para);

The DOM also provides a getattribute () method that works just the opposite of the setattribute () method, which can be used to retrieve a property value.

Set attribute node (SetAttribute ())

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.