element not defined when using "createElement"

L

laredotornado

Hi,

Using advice from this newsgroup, I tried this JS to create new DOM
elements on-the-fly

var divSidebarItem =
document.createElement("div");
divSideBarItem.setAttribute("class",
"sidebarToDo");

but I get a JS error on the second line "divSideBarItem" is not
defined when I try and invoke the "setAttribute" method. Is there a
better way to create an element with attributes or is there something
wrong with the above?

Thanks, - Dave
 
E

Evertjan.

Hi,

Using advice from this newsgroup, I tried this JS to create new DOM
elements on-the-fly

var divSidebarItem =
document.createElement("div");
divSideBarItem.setAttribute("class",
"sidebarToDo");


'divSidebarItem' != 'divSideBarItem'

Wath your cases!
 
E

Evertjan.

Good Man wrote on 21 feb 2007 in comp.lang.javascript:
divSidebarItem isn't an element/node. you've defined it as a command to
CREATE an element.

No, that is not the error, see my other post.
 
E

Evertjan.

Good Man wrote on 21 feb 2007 in comp.lang.javascript:
hmm, good eyes.

is the case change all that is necessary for this to work? Does
"divSideBarItem" become a reference to the created DIV instead of the
command to create the DIV?

Another Q, my good man!

Mind:

When setting the CLASS attribute using this method, set the sName to be
"className", which is the corresponding Dynamic HTML (DHTML) property.
<http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/setattri
bute.asp>

Try this:

=================================

<style type='text/css'>
..sbiStyle {color:red;}
</style>

<div id=d>This is div "d"</div>

<script type='text/javascript'>

var sbi = document.createElement('div');
sbi.setAttribute('className','sbiStyle');

var d = document.getElementById('d');
document.body.insertBefore(sbi, d);

sbi.innerHTML = '<h1>This is the "sbi"</h1>';

</script>

=================================
 
I

Isaac Schlueter

var divSidebarItem = document.createElement("div");
divSideBarItem.setAttribute("class", "sidebarToDo");

but I get a JS error on the second line "divSideBarItem" is not
defined when I try and invoke the "setAttribute" method. Is there a
better way to create an element with attributes or is there something
wrong with the above?

Look carefully, Dave.

divSide b arItem
divSide B arItem

JS is case-sensitive. The B in SideBar is capitalized on the second
line, but not on the first. divSideBarItem is not defined--
divSidebarItem is.

:)
 
R

RobG

hmm, good eyes.

is the case change all that is necessary for this to work? Does
"divSideBarItem" become a reference to the created DIV instead of the
command to create the DIV?

When the call operataor () follows an identifier, javascript will
attempt to execute the object referenced by the identifier. The
return value of executing the function is returned, so:

var aDiv = document.createElement('div');

assigns the result of executing the right hand side to the identifier
on the left. Simply doing:

document.createElement('div');

will create a div and return a reference to it. Since that reference
isn't assigned to anything, the div will immediately become available
for garbage collection and effectively ceases to exist almost from the
instant of its creation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top