setAttributeNS method
9 | ||||
Adds an attribute with the specified namespace, name and value to the current element.
Note: Internet Explorer supports the setAttributeNS method from version 9, but only for HTML documents, not for XML documents.
If an attribute with the same namespaceURI and name exists on the current element, then the setAttributeNS method modifies its value.
The namespace and name are case-sensitive.Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the namespace URI for the attribute. | |||||||
Required. String that specifies the name for the attribute. | |||||||
Required. String that specifies the value for the attribute. |
Return value:
This method has no return value.
Methods for attributes with namespaces:
Name | Browser | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
createAttributeNS |
|
Creates a new attribute node with the specified namespace and name. | ||||||||||
getAttributeNS |
|
Returns the value of the attribute with the specified namespace and name from the current element. | ||||||||||
getAttributeNodeNS |
|
Returns the attribute node with the specified namespace and name from the current element. | ||||||||||
getNamedItemNS |
|
Returns the attribute node with the specified namespace and name from the current attributes collection. | ||||||||||
hasAttributeNS |
|
Returns whether the current element has an attribute with the specified namespace and name or not. | ||||||||||
removeAttributeNS |
|
Removes the attribute with the specified namespace and name from the current element. | ||||||||||
removeAttributeNode | Removes the specified attribute node from the current element. | |||||||||||
removeNamedItemNS |
|
Removes the attribute with the specified namespace and name from the current attributes collection and returns the removed attribute node. | ||||||||||
setAttributeNS |
|
Adds an attribute with the specified namespace, name and value to the current element. | ||||||||||
setAttributeNodeNS |
|
Adds the specified attribute node to the current element. | ||||||||||
setNamedItemNS |
|
Adds the specified attribute node to the current attributes collection. |
Example HTML code 1:
This example illustrates the use of the setAttributeNS method:
|
|||||||
<head> <script type="text/javascript" src="ajax.js"></script> <script type="text/javascript"> var httpRequest = null; function SendRequest () { if (!httpRequest) { httpRequest = CreateHTTPRequestObject (); // defined in ajax.js } if (httpRequest) { // The requested file must be in the same domain that the page is served from. var url = "ns.xml"; httpRequest.open ("GET", url, true); // async httpRequest.onreadystatechange = OnStateChange; httpRequest.send (null); } } function OnStateChange () { if (httpRequest.readyState == 0 || httpRequest.readyState == 4) { if (IsRequestSuccessful (httpRequest)) { // defined in ajax.js Test_CreateAttributeNS (); } else { alert ("Operation failed."); } } } function Test_CreateAttributeNS () { var xmlDoc = ParseHTTPResponse (httpRequest); // defined in ajax.js if (!xmlDoc) return; var itemTags = xmlDoc.getElementsByTagName ("item"); var firstItem = itemTags[0]; if (firstItem.getAttributeNS && firstItem.setAttributeNS) { var color = firstItem.getAttributeNS ("http://help.dottoro.com/NS", "color"); alert ("The value of the color attribute is " + color); firstItem.setAttributeNS ("http://help.dottoro.com/NS", "color", "blue"); var color = firstItem.getAttributeNS ("http://help.dottoro.com/NS", "color"); alert ("The value of the color attribute is " + color); } else { alert ("Your browser doesn't support this functionality!"); } } </script> </head> <body> <button onclick="SendRequest ()">Test the createAttributeNS method</button> </body> |
|||||||
|
|||||||
Did you find this example helpful?
|
Supported by objects:
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
setAttribute
createAttributeNS
getAttributeNS
getAttributeNodeNS
getNamedItemNS
hasAttributeNS
removeAttributeNS
removeAttributeNode
removeNamedItemNS
setAttributeNodeNS
setNamedItemNS
createAttributeNS
getAttributeNS
getAttributeNodeNS
getNamedItemNS
hasAttributeNS
removeAttributeNS
removeAttributeNode
removeNamedItemNS
setAttributeNodeNS
setNamedItemNS
External links:
User Contributed Comments