Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2005, 11:40 PM   PM User | #1
PhotoJoe47
Regular Coder

 
Join Date: Oct 2005
Location: Arizona
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
PhotoJoe47 is an unknown quantity at this point
How to study DOM objects?

I know if I use this code:

var arrTD = document.getElementByType('td')

That arrTD will have an array of "HTML DOM TableData Objects".

Now what is the best way to access each object and see what it contains. I would like see which properties each object has and what their values are. I also guess there could be some other objects within the TD object and maybe even some events and methods.

Now I have been reading an on-line reference about DOM, but I think it is a little dated.

http://www.w3schools.com/htmldom/default.asp

For example I know that a <TD> can contain class="something" but I could not find a property called class, but it did list others like id, align etc.

I would like to learn how to access and view the contents of a DOM object. Any pointers or links to good on-line tutorials or references would be very much appreciated.

Thanks
__________________
"Wish You Enough"

PhotoJoe
Check out my website site
PhotoJoe47 is offline   Reply With Quote
Old 12-20-2005, 12:00 AM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
This is a pretty good place to start: <uri:http://www.w3.org/TR/2003/REC-DOM-Le...t-binding.html>
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 12-20-2005 at 09:06 AM..
liorean is offline   Reply With Quote
Old 12-20-2005, 01:30 AM   PM User | #3
PhotoJoe47
Regular Coder

 
Join Date: Oct 2005
Location: Arizona
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
PhotoJoe47 is an unknown quantity at this point
Quote:
Originally Posted by liorean
This is a pretty good place to start: http://www.w3.org/TR/2003/REC-DOM-Le...t-binding.html
Thanks for the link. I fixed in the quote

So in my example above.

var arrTD = document.getElementByType('td')

To access the <td> that had a property of class="something"

I would loop through the arrTD array search for "something" like this with "i" as the looping variable.

Code:
 
for( i = o; i < arrTD.length -1; i++)
  {
    if (arrTD[i].className == "something")
      {
         alert(arrTD[i].innerText);
      }
   }
Would this display alert message box for each <td> that had a class="something"?
__________________
"Wish You Enough"

PhotoJoe
Check out my website site
PhotoJoe47 is offline   Reply With Quote
Old 12-20-2005, 03:10 AM   PM User | #4
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
no, because you have errors:
Code:
var arrTD = document.getElementsByTagName('td')
for (var i = 0; i < arrTD.length; i++)
  {
    if (arrTD[i].className == "something")
      {
         alert(arrTD[i].innerText);
      }
   }
fci is offline   Reply With Quote
Old 12-21-2005, 12:45 PM   PM User | #5
PhotoJoe47
Regular Coder

 
Join Date: Oct 2005
Location: Arizona
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
PhotoJoe47 is an unknown quantity at this point
Thanks "fci",

That was just a typo on my part I meant 0 not o. My right ring finger did not reach quite far enought. Also I don't think you are suppose to use val inside the for() function.

I have another question for the group. Where can I find an on-line reference for javascript functions that applied to DOM? For example getElementBy? In other words what are all the different thing you can get element by. I have seen quite a few by examples (Name, Type, Id) but I'm sure there are more I don't know about. Also I think there is a create element type function? I have tried doing Google search for some of the getElementBy? types but I only found examples of using just that one type. I'm sure there is an on-line reference I just don't know what to search for.
__________________
"Wish You Enough"

PhotoJoe
Check out my website site
PhotoJoe47 is offline   Reply With Quote
Old 12-21-2005, 06:22 PM   PM User | #6
SpirtOfGrandeur
Regular Coder

 
Join Date: May 2005
Location: Michigan, USA
Posts: 566
Thanks: 0
Thanked 0 Times in 0 Posts
SpirtOfGrandeur is an unknown quantity at this point
Quote:
Originally Posted by PhotoJoe47
Thanks "fci",

That was just a typo on my part I meant 0 not o. My right ring finger did not reach quite far enought. Also I don't think you are suppose to use val inside the for() function.
Yes you sure are. It is just like using it inside a c++ or c for loop. Inside the for each line is set serperatly. and declaring a variable is always better then not declaring a variable.

DOM Elements if you search some of the other pages you should be able to find more information like you are looking for.
__________________
Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.
SpirtOfGrandeur is offline   Reply With Quote
Old 12-22-2005, 04:18 AM   PM User | #7
mulangi
New to the CF scene

 
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
mulangi is an unknown quantity at this point
Quote:
Originally Posted by PhotoJoe47
I would like to learn how to access and view the contents of a DOM object. Any pointers or links to good on-line tutorials or references would be very much appreciated.

Thanks
I have a webpage which can be used exactly for answering this question: what DOM properties are supported by each element? Go here and click the Find out button.

Feedback on the webpage welcome.
__________________
Java, Javascript/DOM tips, help and tutorials:
http://www.hyperfaqs.org
mulangi is offline   Reply With Quote
Old 12-22-2005, 09:06 AM   PM User | #8
PhotoJoe47
Regular Coder

 
Join Date: Oct 2005
Location: Arizona
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
PhotoJoe47 is an unknown quantity at this point
Quote:
Originally Posted by SpirtOfGrandeur
Yes you sure are. It is just like using it inside a c++ or c for loop. Inside the for each line is set serperatly. and declaring a variable is always better then not declaring a variable.

DOM Elements if you search some of the other pages you should be able to find more information like you are looking for.
I guess your right, it's just that my javascript reference book shows examples without using the val. But it might be best practice to use the val keyword the first time you use the variable.

I do have one question thought, when the var is declared inside the for() statement is the scope of the variable limited to just that for loop?

Also thanks for the link to DOM Elements.
__________________
"Wish You Enough"

PhotoJoe
Check out my website site
PhotoJoe47 is offline   Reply With Quote
Old 12-22-2005, 09:08 AM   PM User | #9
PhotoJoe47
Regular Coder

 
Join Date: Oct 2005
Location: Arizona
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
PhotoJoe47 is an unknown quantity at this point
Quote:
Originally Posted by mulangi
I have a webpage which can be used exactly for answering this question: what DOM properties are supported by each element? Go here and click the Find out button.

Feedback on the webpage welcome.
Thanks for the link. It looks very interesting and useful.
__________________
"Wish You Enough"

PhotoJoe
Check out my website site
PhotoJoe47 is offline   Reply With Quote
Old 12-28-2005, 02:32 PM   PM User | #10
Pyth007
Regular Coder

 
Join Date: Sep 2005
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
Pyth007 is an unknown quantity at this point
Quote:
Originally Posted by PhotoJoe47
I do have one question thought, when the var is declared inside the for() statement is the scope of the variable limited to just that for loop?
Yes... which is why it's recomended to declare your varibles in the scope that it's meant for... If you are just using the variable as a counter for the loop then its best to use for(var i...) If you want to use it after the loop finishes, declare the variable before the loop:
Code:
var i;
for(i =0...) { ... }
i+=7;
...
If you do not declare your variable with the var keyword, then the variable's scope becomes global, which means any function now has access to it!
__________________
If you want answers, write a smart question.

Yes, someone probably does know how...

Oh, and if you want to learn, STFW!
Pyth007 is offline   Reply With Quote
Old 12-28-2005, 04:18 PM   PM User | #11
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
in fact, the increment loop is to be set as a local variable. If a global, it is for no use. It will be always equal with the limit, or a null...

var i;
somefunction(){
for(i =0;i<somelimit;i++){
... something...
}
}

Now the i variable will be null, if the function somefunction() was not triggered, and will have the value=somelimit if that function was triggered before.

So... Why using a loop increment as global?... No use, in my opinion...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 12-28-2005, 05:18 PM   PM User | #12
dumpfi
Regular Coder

 
Join Date: Jun 2004
Posts: 565
Thanks: 0
Thanked 18 Times in 18 Posts
dumpfi will become famous soon enough
Quote:
Originally Posted by Pyth007
Quote:
Originally Posted by PhotoJoe47
I do have one question thought, when the var is declared inside the for() statement is the scope of the variable limited to just that for loop?
Yes... which is why it's recomended to declare your varibles in the scope that it's meant for...
This is not true.

If you declare a variable with var it becomes local to the function in which it is declared or global, if it isn't declared within a function or if it isn't declared with var.

However, there is nothing special in declaring a variable within a loop:
Code:
function a()
{
	// declaring i inside a loop
	for(var i = 0; i < 50; ++i)
	{
		if(i == 20)
		{
			break;
		}
	}
	// accessing i outside of the loop --> will yield "20"
	alert(i);
}
a();
// accessing i outside of the function in which it is declared --> error: i is not defined
alert(i);
dumpfi
dumpfi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:34 PM.


Advertisement
Log in to turn off these ads.