edbrowse-dev - development list for edbrowse
 help / color / mirror / Atom feed
From: Chris Brannon <chris@the-brannons.com>
To: Edbrowse-dev@lists.the-brannons.com
Subject: Re: [Edbrowse-dev] setAttribute
Date: Tue, 06 Oct 2015 19:26:42 -0700	[thread overview]
Message-ID: <87si5nmn19.fsf@mushroom.localdomain> (raw)
In-Reply-To: <20150906201020.eklhad@comcast.net> (Karl Dahlke's message of "Tue, 06 Oct 2015 20:10:20 -0400")

Karl Dahlke <eklhad@comcast.net> 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.

<quote>
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.
</quote>

-- Chris

      reply	other threads:[~2015-10-07  2:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-06 17:43 Karl Dahlke
2015-10-06 23:51 ` Kevin Carhart
2015-10-07  0:10   ` Karl Dahlke
2015-10-07  2:26     ` Chris Brannon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87si5nmn19.fsf@mushroom.localdomain \
    --to=chris@the-brannons.com \
    --cc=Edbrowse-dev@lists.the-brannons.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).