Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementById Table Row Problem

Status
Not open for further replies.

RAxel

MIS
Sep 8, 2005
121
US
Hey all, I'm trying to give this function some automation instead of hard coding everything.

I have 2 functions

Function 1
Code:
function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

Function 2
Code:
function reconfig(obj,start,end) {
    var sname=obj.name;			
    var sid=obj.id;
    var strid=sid+"_id";
    var re=new RegExp("sid");
    var oform=obj.form;
    for (var i=start; i<end;i++) {
        var oelem=oform.elements[i];
.
.
        if (oelem.type=="checkbox" && !re.test(oelem.id)) {
            oelem.checked=false;
		     var rowname = oelem.name+"_id";
		if (isdefined(document.getElementById(rowname))) {
		     document.getElementById(rowname).style.display = 'none';
			}
        }
.
.
     }    	
}

I'm new to javascript and someone on this forum (I think) helped me with this problem I had, so I don't want to take credit for the code above.

At any rate, what I'm sending to function reconfig, are elements in a form (radio buttons, checkboxes, textboxes, etc...). Here's an illustration below on my current setup:

RadioButton1 Name=Q01 ID=Q01A
TableRoW ID=Q01A_id
Checkbox Name=Q01A01C ID=Q01A_id
TableRow ID=Q01A01C_id
TextBox Name=Q01A01C01 ID=Q01A_id
RadioButton2 Name=Q01 ID=Q01E

At the start, RadioButton2 is selected and all the elements under RadioButton1 are hidden (display='none'). When I select RadioButton1, and select all the elements under it and then re-select RadioButton2, all the elements under RadioButton1 are hidden (which is good). However, when I select RadioButton1 again, the TextBox appears under the checkbox (this shouldn't happen unless the checkbox is checked).

I think the line where I use isdefined to see if the row name is there is the problem. My thought process is, if the rowname is defined, then hide that row, else, don't.

How can I use these functions to hide a table row without hardcoding the tablerow id into the function?

If you need more info, just ask, I'll post the rest of the function if needed, but, I didn't want to clutter it up.

Thanks!

 
both your checkbox and textbox have an id of "Q01A_id" - ids must be unique

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Thanks for your reply.

I'm not so sure that that is the problem, but then again I'm a noob. I've been going over the code (I'm at home now, not at work) and I don't see how that has an impact on the isdefined line.

The code unchecks everything and makes the textbox blank, it's just that the tablerow will not be made hidden (the one that has the textbox below it, ID=Q01A01C_id).

Am I not following how the function acts?
 
Regardless of whether you think it might have any bearing on the problem, why not try it and find out?

Jeff is very right - IDs [!]must[/!] be unique - so if your code has non-unique IDs and isn't working, then your brain should be thinking "maybe he's right".

Try it, don't try it - whatever. But (and especially as a self-confessed "noob") if you don't want to bother to fix an obvious bug after getting some good solid advice, then don't expect much other help, as people will probably not want to bother wasting their time to help you out if you are not even going to attempt to try suggestions.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
this is another shot in the dark without some working example code, but in firefox table rows must be set to display:table-row to appear - in IE it's display:block because it doesn't understand the correct CSS specification

try posting a small working example (showing the defect) or provide a link to one


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Opposed to what BillyRay said, I really do appreciate your time jemminger. I have changed the ID's but that hasn't worked so far. I said that I don't know if that is the problem simply because I had other checkboxes under RadioButton1 with the same ID. But, in any case, here is the whole function I use:

Code:
function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  true: false;
}
I swapped true and false

Code:
function reconfig(obj,start,end) {
    var sname=obj.name;			
    var sid=obj.id;
    var strid=sid+"_id";
    var re=new RegExp("sid");

   	if (isdefined(document.getElementById(strid))) {
		document.getElementById(strid).style.display=obj.checked?"block":"none";
	}
		
    var oform=obj.form;
    for (var i=start; i<end;i++) {
        var oelem=oform.elements[i];
        if (oelem.type=="radio" && oelem.name==sname) {    
            document.getElementById(oelem.id+"_id").style.display=oelem.checked?"block":"none";
        }
		if (oelem.type=="radio" && oelem.checked==true && oelem.name!=sname) {
			oelem.checked = false;
			if (isdefined(document.getElementById(oelem.id+"_id"))) {
				var row = document.getElementById(oelem.id+"_id");
				row.style.display = 'none';
			}
		}
        if (oelem.type=="checkbox" && !re.test(oelem.id)) {
            oelem.checked=false;
			var rowname = oelem.name+"_id";
			if (!isdefined(document.getElementById(rowname))) {
				document.getElementById(rowname).style.display = 'none';
			}
        }
		if (oelem.type=="text")  {
	   	 oelem.value='';
		}
    }    	
}

Here's a step by step process I did:

1) I've selected RadioButton1: Name=Q01 ID=Q01A
TableRow ID=Q01A_id
2) Checked off all three checkboxes under it with the last checkbox displaying a textbox:
Checkbox1 Name=Q01A01A ID=Q01A01A_id
Checkbox2 Name=Q01A01B ID=Q01A01B_id
Checkbox3 Name=Q01A01C ID=Q01A01C_id
TableRow ID=Q01A01C01_id
Textbox Name=Q01A01C01 ID=Q01A01C01A_id
3) I click a different RadioButton: Name=Q01 ID=Q01E

RadioButton1 is unchecked
All checkboxes are unchecked
The textbox is clear
The checkboxes and textbox are all made hidden

4) I re-click RadioButton1 and everything appears. I only want the checkboxes to reappear until I re-check the last checkbox.

That's the only error I seem to be having.

I apologize for my sounding rude but it wasn't meant as such. I always try everything everyone says on this board because it's the best place to get advice.

Thanks for your time!
 
For now, I think the problem is solved. I got rid of the isdefined function and changed this:

Code:
if (isdefined(document.getElementById(rowname))) {
                document.getElementById(rowname).style.display = 'none';
            }

to this

Code:
if (document.getElementById(rowname) != undefined) {
				document.getElementById(rowname).style.display = 'none';
			}

Thanks for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top