473,657 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

element not defined when using "createElem ent"

Hi,

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

var divSidebarItem =
document.create Element("div");
divSideBarItem. setAttribute("c lass",
"sidebarToD o");

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

Thanks, - Dave

Feb 21 '07 #1
7 11108
"la***********@ zipmail.com" <la***********@ zipmail.comwrot e in
news:11******** **************@ q2g2000cwa.goog legroups.com:
Hi,

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

var divSidebarItem =
document.create Element("div");
divSideBarItem. setAttribute("c lass",
"sidebarToD o");

but I get a JS error on the second line "divSideBarItem " is not
defined when I try and invoke the "setAttribu te" method. Is there a
better way to create an element with attributes or is there something
wrong with the above?
divSidebarItem isn't an element/node. you've defined it as a command to
CREATE an element.

Feb 21 '07 #2
la***********@z ipmail.com wrote on 21 feb 2007 in comp.lang.javas cript:
Hi,

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

var divSidebarItem =
document.create Element("div");
divSideBarItem. setAttribute("c lass",
"sidebarToD o");

'divSidebarItem ' != 'divSideBarItem '

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

Thanks, - Dave


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 21 '07 #3
Good Man wrote on 21 feb 2007 in comp.lang.javas cript:
"la***********@ zipmail.com" <la***********@ zipmail.comwrot e in
news:11******** **************@ q2g2000cwa.goog legroups.com:
>Hi,

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

var divSidebarItem =
document.creat eElement("div") ;
divSideBarItem. setAttribute("c lass",
"sidebarToDo") ;

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

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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 21 '07 #4
"Evertjan." <ex************ **@interxnl.net wrote in
news:Xn******** ************@19 4.109.133.242:
>divSidebarIt em 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.
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?

thanks...

Feb 21 '07 #5
Good Man wrote on 21 feb 2007 in comp.lang.javas cript:
"Evertjan." <ex************ **@interxnl.net wrote in
news:Xn******** ************@19 4.109.133.242:
>>divSidebarIte m 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.

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/a...thods/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.create Element('div');
sbi.setAttribut e('className',' sbiStyle');

var d = document.getEle mentById('d');
document.body.i nsertBefore(sbi , d);

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

</script>

=============== =============== ===
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 21 '07 #6
On Feb 21, 9:50 am, "laredotorn...@ zipmail.com"
<laredotorn...@ zipmail.comwrot e:
>
var divSidebarItem = document.create Element("div");
divSideBarItem. setAttribute("c lass", "sidebarToD o");

but I get a JS error on the second line "divSideBarItem " is not
defined when I try and invoke the "setAttribu te" 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.

:)

--
Isaac Z. Schlueter
http://isaacschlueter.com

Feb 23 '07 #7
On Feb 22, 4:25 am, Good Man <h...@letsgo.co mwrote:
"Evertjan." <exjxw.hannivo. ..@interxnl.net wrote innews:Xn****** **************@ 194.109.133.242 :
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.

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.create Element('div');

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

document.create Element('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.
--
Rob

Feb 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7487
by: MiW | last post by:
Hi I'm making a kind of JavaScript-based library for creating SVG objects/elements. And I hit the wall... Every element (rect, circle etc.) can be created using *.createElement, then *.setAttribute and *.appendChild. Except "image" element. When 'image' element is created like this, it doesn't display anything. When I create that element with the same attributes in SVG code itself, it works perfectly. It works even if I create it with
5
12435
by: NeilL | last post by:
In the XML document I'm trying to create I do the following elem = _doc.CreateElement("Author"); elem.InnerText = "something"; parentElem.AppendChild(elem); Thiw works properly however the resulting XML file has the following <Author xmlns="">something</Author>
4
62207
by: millw0rm | last post by:
Hi wats wrng with this code??? it works fine on IE6 but not on FireFox 1.5??? var anOption = document.createElement("OPTION"); document.getElementById("category").options.add(anOption); anOption.innerText = "NewElement"; anOption.Value = "99";
7
8004
by: christian | last post by:
hi there! i've the following piece of html: <div id="tbl"></div> and i'm trying to create an object with the following javascript code: var objDiv = document.getElementById("tbl"); var obj = document.createElement("object");
1
2658
by: k386 | last post by:
I am having trouble creating a hidden form field, or least having it accessible from getElementById. Rough overview: I am reading an XML file through XMLhttpRequest and outputting each item into a unique div tag, which has a dynamically generated name, then appending each div tag to an existing div tag. This is working A-OK. Here's the problem: Basically so I can show a general section - ie. Bookmarks, Blog Posts, etc - then show or...
2
2518
by: goodevilgenius | last post by:
I have a script that should insert a youtube video into the page. But when I ran it, the video wouldn't show up. (The script inserts a number of things, and the video was the only thing not to appear) So, I open firebug, and check the DOM. It's not in there. So, I type in "var node = document.createElement("object");" to see what was going on. It then reports node as a function, but it still has all the DOM methods and properties (appendChild,...
6
19245
by: Amzul | last post by:
hello all i did this table useing createElement(); i am trying to insert html code to the <td> to put img and links but so far no luck any one have an idea on how it shouled b done? the second qustion is how i can desing the hieght of the td here is snippet var cell = document.createElement("td");
4
23889
by: tswaters | last post by:
Alright, so generally I'm using document.createElement("IMG").... but, I've noticed something just recently that made me switch to "new Image()" What I'm doing is creating a photo gallery of sorts, where i basically want to supply a total # of images, rows/cols max thumb/full image and let the script do the work. The solution I've opted for is to load the images once and change their size / position / visibility to show "thumb galleries" or...
7
1831
by: r_ahimsa_m | last post by:
Hello, I am learning JavaScript. I have a table on HTML page:                 <table id="announcement_fields" border="0">                 <tbody>                 <tr>                 <td><span class="obligatory">Offer type</span>:</td>                 <td>                         <select name="offer_type" onchange="ShowAnnouncementRows();">
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.