SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    finding ids using javascript

    Hi javascript gurus,

    I have a table with a structured as below. What I want to do is place the id abc into an array like so myarray=[a,b,c];
    I know that document.getElementById goes to the table called waterloo and getElementsByTagName('td') gets all the tds
    then I am looping over them all. All the alert gives me are three commas like so ,,,

    Any help would be great
    Code:
    <table id="waterloo">
    <tr>
             <td id="a"></td>
             <td id="b"></td>
              <td id="c"></td>
    </tr>
    </table>
    <script>
       var  myTdId =[];
        
    	var x= document.getElementById(formId).getElementsByTagName('td');
    
    	for (var i=0; i<x.length; i++) {
       	    if (x[i].hasOwnProperty) {
                       myTdId.push(temp[i]);
      		}
    	}
              alert (myTdId);
    </script>

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var x= document.getElementById(formId).getElementsByTagName('td');
    The above line is getting the td's only from within the element whose id = formId. What is the value of formId?

    Also, where is temp[i] defined in your code?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the relpy

    Sorry formId is waterloo and I change x now I get the following in the alert box

    [object HTMLTableCellElement],
    [object HTMLTableCellElement],
    [object HTMLTableCellElement],


    Code:
        var formId="waterloo"
        var  myTdId =[];
        
    	var x= document.getElementById(formId).getElementsByTagName('td');
    
    	for (var i=0; i<x.length; i++) {
      	    if (x[i].hasOwnProperty) {
                       myTdId.push(x[i]);
      		}
    	}
             alert (myTdId);

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're not using hasOwnPeoperty correctly.


Bookmarks

Posting Permissions

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