Thomas said:
RobG wrote:
Thomas said:
RobG wrote:
briggs wrote:
c = document.createElement("DIV"); // Create 'div' element.
c.id = "ch";
c.name = "ch";
[...]
[...]
If you check the elements, they have a 'name' attribute
False. The element object created is added a `name' property.
c.getAttribute("name") // null
Consider the following: [...]
<div id="steve">name is fred, id is steve</div>
<script type="text/javascript">
var x = document.getElementById('steve');
x.name = 'fred';
alert(
'hasAttribute name? ' + (x.hasAttribute('name'))
+ '\nname in x? ' + ('name' in x)
+ '\nValue of x.name? ' + x.name
);
</script>
[...]
I was talking about creating element objects through DOM scripting and
what happens when they are added a property that does not have a Valid
corresponding attribute.
You are talking about referring to existing elements in _markup_ via
element objects and what happens when that element objects are added
a property that does not have a Valid corresponding attribute.
I was highlighting differences in outcomes depending on how it is
attempted for the purpose of discussion, not contradiction.
As for the code of the OP,
c.hasAttribute("name")
correctly yields `false' in Firefox 1.5 if called afterwards.
'correct' on what basis? Can you explain why 'false' is the correct
response?
==Some further discussion==
In Firefox, attributes can be added to an element:
1. in the HTML source,
2. using the element's setAttribute method, and
3. using JavaScript dot notation.
The first two methods will add the attribute to the HTML element's
attribute object, making them available to other methods such as
getElementById or getElementsByName.
Using dot notation will only add an attribute to the element's
attributes object if the element has an attribute with the same name
defined in the relevant DTD.
This means that an ID can be added to a DIV's attributes object using:
divRef.id = 'divId';
divRef.hasAttribute('id') // returns true
But since name is not an attribute defined in the HTML 4 DTD, a name
attribute isn't added to divRef's attributes object if dot notation is used:
divRef.name = 'divName';
divRef.hasAttribute('name') // returns false
But a name attribute can be added to the attributes object using
setAttribute:
divRef.setAttribute('name','divName');
divRef.hasAttribute('name') // returns true
Now hasAttribute('name') will return true, getElementsByName will find
the div and innerHTML reveals a name attribute in the HTML.
The results from IE 6 are quite different, it does not allow the 'name'
attribute to be added to a DIV element's attributes object by any of the
above methods.
We can, even in an ambiguous sense.
'Ambiguous' infers that you think it is unreliable for a reason
different to mine.
I explained that different results are obtained using exactly the same
code in different browsers and that how the attribute is attached can
affect the outcome in ways that are not obvious or might not be expected.
Do you have a different reason for saying so?.
1. Ignoring innerHTML, innerText and other proprietary approaches.