From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (71-38-131-64.ptld.qwest.net [71.38.131.64]) by hurricane.the-brannons.com (Postfix) with ESMTPSA id 5FA707903E for ; Tue, 6 Oct 2015 19:23:08 -0700 (PDT) From: Chris Brannon To: Edbrowse-dev@lists.the-brannons.com References: <20150906134334.eklhad@comcast.net> <20150906201020.eklhad@comcast.net> Date: Tue, 06 Oct 2015 19:26:42 -0700 In-Reply-To: <20150906201020.eklhad@comcast.net> (Karl Dahlke's message of "Tue, 06 Oct 2015 20:10:20 -0400") Message-ID: <87si5nmn19.fsf@mushroom.localdomain> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Edbrowse-dev] setAttribute X-BeenThere: edbrowse-dev@lists.the-brannons.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Edbrowse Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Oct 2015 02:23:13 -0000 Karl Dahlke writes: > Yes so it seems, so I have to query variables as needed anyways, > so no need for setAttribute to do anything more than > > function setAttribute(s,t) { this[s] = t; } I'm just going to quote a whole big wall of text from JavaScript, the Definitive Guide. Hopefully it's more helpful than not. Getting and Setting Non-HTML Attributes As described above, HTMLElement and its subtypes define properties that correspond to the standard attributes of HTML elements. The Element type also defines get Attribute() and setAttribute() methods that you can use to query and set nonstandard HTML attributes and to query and set attributes on the elements of an XML document: var image = document.images[0]; var width = parseInt(image.getAttribute("WIDTH")); image.setAttribute("class", "thumbnail"); The code above highlights two important differences between these methods and the property-based API described above. First, attribute values are all treated as strings. getAttribute() never returns a number, boolean, or object. Second, these methods use standard attribute names, even when those names are reserved words in JavaScript. For HTML elements, the attribute names are case insensitive. Element also defines two related methods, hasAttribute() and removeAttribute(), which check for the presence of a named attribute and remove an attribute entirely. These methods are particularly useful with boolean attributes: these are attributes (such as the disabled attribute of HTML form elements) whose presence or absence from an element matters but whose value is not relevant. If you are working with XML documents that include attributes from other namespaces, you can use the namespaced variants of these four methods: getAttributeNS(), setAttributeNS(), hasAttributeNS(), and removeAttributeNS(). Instead of taking a single attribute name string, these methods take two. The first is the URI that identifies the namespace. The second is usually the unqualified local name of the attribute within the namespace. For setAttributeNS() only, however, the second argument is the qualified name of the attribute and includes the namespace prefix. You can read more about these namespace-aware attribute methods in Part IV. Dataset Attributes It is sometimes useful to attach additional information to HTML elements, typically when JavaScript code will be selecting those elements and manipulating them in some way. Sometimes this can be done by adding special identifiers to the class attribute. Other times, for more complex data, client-side programmers resort to the use of nonstandard attributes. As noted above, you can use the getAttribute() and set Attribute() methods to read and write the values of nonstandard attributes. The price you pay, however, is that your document will not be valid HTML. HTML5 provides a solution. In an HTML5 document, any attribute whose name is lowercase and begins with the prefix "data-" is considered valid. These "dataset attributes" will not affect the presentation of the elements on which they appear and they define a standard way to attach additional data without compromising document validity. HTML5 also defines a dataset property on Element objects. This property refers to an object, which has properties that correspond to the data- attributes with their prefix removed. Thus dataset.x would hold the value of the data-x attribute. Hyphenated attributes map to camel-case property names: the attribute data-jquery-test becomes the property dataset.jqueryTest. -- Chris