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
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question how to access value of drop down boxes in specific cells in a table?

    Hi, I'm having trouble with trying to access a selected value of a drop down menu in a table. I have menus in columns 3 and 4, named cred and grade respectively, from rows 2 onwards, and I'm using a function to access them. How do I access the value's for manipulating other variables?

    Here is what I have so far:

    Code:
    function f(tableID){
        var len = document.getElementById(tableID).rows.length;
    				
        for(var i = 1; i<len; i++){						
           var c = document.getElementById(tableID).rows[i].cells[2].childNodes[0];
            credits = c.options[c.selectedIndex].text; 
        }
    }
    Your help would be really appreciated

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Did you try this?
    Code:
    theValue = c.value;

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    yeah, if I see the value by using alert it comes up as undefined :S

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Please show us the underlying HTML structure of the respective select

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by sophie_pocket View Post
    Code:
    function f(tableID){
        var len = document.getElementById(tableID).rows.length;
    				
        for(var i = 1; i<len; i++){						
           var c = document.getElementById(tableID).rows[i].cells[2].childNodes[0];
            credits = c.options[c.selectedIndex].text; 
        }
    }
    You can't rely on childNodes[0] referring to the first tag inside the cell.

    Assuming that your addressing is correct, you could try: cells[ 2 ].getElementsByTagName( 'select' )[0];

  • #6
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks. I'm now able to get the value of the drop down menu in the first row, but I still can't get them for the other rows, all the values that go into my array duplicate the first row. My values for the first dropdown menu arguments[2] are "10" and "20" so if I wanted my array to be [10,20] it would be [10,10].

    I'm passing in my selects' id's to the function as the 3rd and 4th arguments => arguments[2] & arguments[3].

    My new code is:

    Code:
    var len = document.getElementById(arguments[1]).rows.length;
    var cMenus = [];
    var gMenus= [];
    var a = 0;
    for(var j = 1; j<len; j++){
    	for(var i = 2; i<arguments.length; i++){
    		var c = document.getElementById(arguments[2]);
    		var g = document.getElementById(arguments[3]);
    						
    		cMenus[a] = c.options[c.selectedIndex].value;
    		gMenus[a] = g.options[g.selectedIndex].value;
    						
    	}
    	a++;
    }
    Last edited by sophie_pocket; 12-12-2011 at 02:28 PM.

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    var c = document.getElementById(arguments[2]);
    var g = document.getElementById(arguments[3]);
    This indexing is fixed, so will always reference the same element.

  • #8
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    well there are only 4 arguments and the last 2 reference the name of the select. dunno how to get these selects for the other rows

  • #9
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have updated my question using another thread as I was unable to edit or delete this one
    Last edited by sophie_pocket; 12-22-2011 at 08:12 PM.


  •  

    Posting Permissions

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