- Code: Select all
<div name='section1'>
<p name='paragraph1'></p>
</div>
Examples of other universal attributes are 'id' and 'class'.
Why would this be useful? Well it may not be of use to static pages, but for pages where parts of the page are dynamically set or updated with content it would be very useful. In JavaScript it would be very nice to have access to an object containing all the named elements on a page.
Currently the 'name' attribute can be used with forms. So this works:
- Code: Select all
<form name='form1'>
<input name='hello'>
</form>
<script>
document.form1.hello.value = 'Hello World'
</script>
But it would be very nice if this also worked:
- Code: Select all
<div name='section1'>
<p name='paragraph1'></p>
</div>
<div name='section2'>
<p name='paragraph1'></p>
</div>
<script>
document.section1.paragraph1.innerHTML = 'Hello World'
</script>
Actually it would be better if the DOM 'document' object had a 'names' property to avoid collision of user selected names with reserved document names. The above script would then use:
- Code: Select all
<script>
document.names.section1.paragraph1.innerHTML = 'Hello World'
</script>
This would open the door for allowing content in a document to be populated using JavaScript code that is passed only an object reference and the does not know the names of the elements on the page. The code can determine the element names using the keys of the associative arrays in the object that was passed to it. In the context of MVC the model could return a JavaScript object and populating the content of the View could be done in JavaScript by the client.
Why is the 'id' or 'class' attribute not sufficient? Mainly because they do not support a nested structure or are being used for other purposes. The value of the 'id' attribute must be unique within the document so it cannot support a nested structure. The 'class' attribute is used for specifying the style of the elements and should not be overloaded to also specify the content of the elements.
The three components of a page are: layout, style and content. The universal 'id' attribute is good for providing a handle to the top level layout elements in a page. The universal 'class' attribute is good for specifying the style (such as color, font, size, etc) of the elements. There is a need for a universal attribute like 'name' to be able to get a handle for elements where content can be dynamically modified.