Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help : getElementsByTagName

    Hello , i've a small question .

    does the function getElementsByTagName work correctly in IE , Molliza , FireFox , Netscape ?

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    do you understand what getElementByTagName does?

    it doesnt actually grab an element by its name attribute.
    it grabs the elements type.
    example:

    <A href=#stahere id=a1 name=a1></a>
    it grabs " A "
    <td id=td1 name=td1></td>
    it grabs " td "


    it does work in internet explorer and netscape. as far as I've heard it works in mozilla. I do not know about firefox.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yes, it works fine in all browsers. It returns an array of referencess to
    all the elements in a page of a certain type.

    For example,
    Code:
    var links=document.getElementsByTagName('a');
    will create an array with references to all the links within your page.

    So if you want to do anything, you need to loop through for the length
    of the array, and apply your changes to each item individually.


    That said, what is it specifically you are having troubles with?
    --Jon.

  • #4
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks all !

    Because i will use this function in my script , and i want it work correctly in every browsers .

    Thansk (again )

  • #5
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi , i've written a function to get all child elements by tag name .

    Code:
    function ElementByTag(tag , bdocument){
    	if (!bdocument) bdocument = document;
    	elements = bdocument.getElementsByTagName(tag);
    	return_elements = new Array;
    	i = 0;
    	while (true) {
    		if (!elements[i]) break;
    		return_elements[i] = elements[i];
    		i++;
    	}
    	return return_elements;
    }
    It works correctly in Opera , NetScape , IE and FireFox .

    But i don't know if it will work in Molliza or other browsers and is there some mistakes in the function ?

    Can someone help me ?

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    If it works with Firefox, it is assumed to work with other Gecko-based browsers as they have the same codebase.

    Your code is correct but you should declare the variables with var keyword to make them local.
    Code:
    function ElementByTag(tag , bdocument){
    	if (!bdocument) bdocument = document;
    	var elements = bdocument.getElementsByTagName(tag);
    	var return_elements = new Array();
    	var i = 0;
    	while (elements[i]) {
    		return_elements[i] = elements[i];
    		i++;
    	}
    	return return_elements;
    }
    The while-loop can also be changed to for-loop.
    Code:
    function ElementByTag(tag , bdocument){
    	if (!bdocument) bdocument = document;
    	var elements = bdocument.getElementsByTagName(tag);
    	var return_elements = new Array();
    	var len = elements.length;
    	for (var i=0; i<len; i++) {
    		return_elements[i] = elements[i];
    	}
    	return return_elements;
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks a lot

  • #8
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *edited till i study opera a bit more*
    Last edited by SpirtOfGrandeur; 05-31-2005 at 04:13 PM.

  • #9
    New Coder
    Join Date
    May 2005
    Location
    Vietnam
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Because : The return value of this function in Opera is different from IE , and also other browsers .

    I want to have a function which return only the child elements , no misc data or information (to use in for ... in


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •