Jump to content

createElement Attributes


ApocalypeX

Recommended Posts

I've been always baffled by attributes of elements when I create and element.For examplevar ele = document.createElement('a');ele.href = "/"; //worksele.style = "display:none;" //component unavailableele.setAttribute('style', 'display:none'); //correctIs it to do with an elements default attributes? That ever element automatically has a id, class etc. attribute for it except it's null when not used. But other attributes like onevents and style etc. are not part of the element/object when instantiated/Instanced and have to be assigned to the element/object?It's not really a problem, I'm just interested :)

Link to comment
Share on other sites

For style attributes in particular, the correct syntax would be ele.style.display = "none";
That's another one that confuses me.That method annoys me and then confuses me even more. Why is the style object not allowed to be changed by .style yet I can access one of it's properties even though it's not set! I admit ele.style.attribute is useful but ele.style would be so much more expandable.
Link to comment
Share on other sites

Why is the style object not allowed to be changed by .style yet I can access one of it's properties even though it's not set!
You said it right there. It's an object, not a string, you're trying to set it to a string. This would work also:ele.style = {display: 'none'};I'm not quite sure what effect that would have on the element (it would remove every other style property), but it's legal syntax.
Link to comment
Share on other sites

You said it right there. It's an object, not a string, you're trying to set it to a string. This would work also:ele.style = {display: 'none'};I'm not quite sure what effect that would have on the element (it would remove every other style property), but it's legal syntax.
Ahhhhh! Yes that makes sense now. Thanks.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...