473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DOM question: value of childnode

hey all,

i'm very new to DOM and javascripting. I found a tutorial somewhere and i was playing around with it but it got stuck on getting the value of a node that i just append at the end. It returns me an empty string when i try to get the node value. I've looked around but no luck making my code work. Any help will be appreciated, thanking in advance! Please see the code below

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>JavaScript</title>
  3. </head>
  4.  
  5. <body>
  6.   <div style='position:absolute; width:150px; height:100px;overflow:auto' id = 'scroll_text'> </div>
  7.   <script language='javascript'>
  8.  
  9.   function move_up() {
  10.  
  11.     var mycars = new Array()
  12.     mycars[0] = "Saab"
  13.     mycars[1] = "Volvo"
  14.     mycars[2] = "BMW"
  15.  
  16.     var obj = document.getElementById("scroll_text")
  17.  
  18.     for (i=0;i<mycars.length;i++)
  19.     {
  20.         var mytext=document.createTextNode(mycars[i] + " ")
  21.  
  22.         obj.appendChild(mytext)
  23.  
  24.     }
  25.  
  26.     alert(obj.firstChild.nodeType)
  27.     var ochildNodes = obj.childNodes
  28.     var len = ochildNodes.length
  29.     alert(len)
  30.  
  31.     alert(obj.firstChild.nodeValue) // the following line returns an empty string
  32.  
  33. //]ultimately i would like to do this
  34.     //obj.firstChild.style.backgroundColor="#0000FF"
  35.   }
  36.   move_up()
  37. </script>
  38. </body>
  39. </html>
Jun 27 '07 #1
5 1681
pbmods
5,821 Expert 4TB
Heya, msalman. Welcome to TSDN!

Hint: obj.firstChild.nodeValue is a space character, not an empty string ~_^
Jun 27 '07 #2
Heya, msalman. Welcome to TSDN!

Hint: obj.firstChild.nodeValue is a space character, not an empty string ~_^
hmm, that's interesting. could you please explain why? Is it because that the inital value inside div is a space character? How can i change the background color of a specific child node because the following code of line doesn't work
Expand|Select|Wrap|Line Numbers
  1. ochildNodes[0].style.backgroundColor="#0000FF"
thank you
Jun 27 '07 #3
acoder
16,027 Expert Mod 8TB
hmm, that's interesting. could you please explain why? Is it because that the inital value inside div is a space character?
In short, yes.
How can i change the background color of a specific child node because the following code of line doesn't work
Expand|Select|Wrap|Line Numbers
  1. ochildNodes[0].style.backgroundColor="#0000FF"
thank you
You can't change the text node, but you can add say, a span, and then seting the background colour to that, e.g.
Expand|Select|Wrap|Line Numbers
  1. var span = document.createElement("span");
  2. span.appendChild(mytext);
  3. obj.appendChild(span);
  4.  
then
Expand|Select|Wrap|Line Numbers
  1. ochildNodes[1].style.backgroundColor="#0000FF"
Jun 28 '07 #4
In short, yes.

You can't change the text node, but you can add say, a span, and then seting the background colour to that, e.g.
Expand|Select|Wrap|Line Numbers
  1. var span = document.createElement("span");
  2. span.appendChild(mytext);
  3. obj.appendChild(span);
  4.  
then
Expand|Select|Wrap|Line Numbers
  1. ochildNodes[1].style.backgroundColor="#0000FF"
thanks for the help, it worked
Jun 28 '07 #5
acoder
16,027 Expert Mod 8TB
No problem, you're welcome. Post again any time if you have more problems.
Jun 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tim:.. | last post by:
Hi Possibly a very simple question but how do I get a value out of an XML document so I can play with it in ASP E.G: <Name>Tom</Name How do I pull the work tom into asp Thanks
9
by: gor | last post by:
I would like to find out how javascript can change some text (string) inside <DIV> tag with event onClick. That i want to solve with function callWrite(); ... so you can see in the code. The thing...
2
by: Eirik M. | last post by:
Can anyone from M$ or anyone else for that matter explain to me the reasoning behind the intended usage for this property? I was quite surprised to find that this property may be null depending on...
4
by: Kent Boogaart | last post by:
Hi, One day I will fully understand XML namespaces and much happiness and partying will ensue. Until then, I must pick the brains of those in the know... If I have an XmlNode and I want to...
3
by: Jon Paal | last post by:
looking for insight on how to make this javascript solution firefox compatible. http://www.codeproject.com/useritems/HTMLFixedHeaders.asp any tips / insight would be helpful
3
by: Saurabh | last post by:
Hi, my xml is: <ROOT> <FILE NAME="filename">CDATA</FILE> </ROOT> this xml is argument to a function in C#. i want to get the filename in a string variable. how do i do it???...
7
by: gk | last post by:
XML ====== <bookstore> − <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price>
6
by: andy | last post by:
say i currently have xml like so... <node> <Date>20080507</Date> <ChildNode /> <ChildNode /> </node> <node> <Date>20080507</Date> <ChildNode />
2
by: akshaycjoshi | last post by:
I have got one treeview contol in which i create nodes dynamically. I want to set a tool tip for each of the node created. I went throught this tutorial How to add a ToolTip to a TreeNode in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.