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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple getElementById

Status
Not open for further replies.

bleak7

Programmer
Apr 3, 2009
1
I have a 'confirm' form that hides a table when a certain field contains a null value.

I actually need 3 fields to be null in order for the table not to show rather than one.

here part of the working code.

Code:
	if (document.getElementById('FIELDNAME').value=='') {
		document.getElementById('ID_OF_TABLE').style.display='none';
	}

How or what do i do to allow for 3 (or multiple) fields?

This is doing my hedad in :)

I know getElementById is most likely not the way to go as it only allows for one.

Thanks in advance for any help.

 
are all of the fields/elements the same name? names can be the same, id can be different, so you can address each individually the same way you supplied.

if they have to have same ID whether or not the name is the same you can use something like the following:


Code:
function hideshow(obj) 
    {
        var toggle = document.all[obj];
        if (toggle != null)
        {
            for (i=0;i<toggle.length;i+=1)
            {
            	toggle[i].style.display = (toggle[i].style.display != 'none' ? 'none' : '' );
            }
        }
        else
        {
            var toggle = document.getElementById(obj);
            if (toggle != null)
            {
              	toggle.style.display = (toggle.style.display != 'none' ? 'none' : '' );
            }
            else
            {
                alert("No data to display for "+obj);
            }
        }
    }

but as most of us know document.all isn't very transportable.

more than likely would end up being a parent/childnode search and array build for all id's same as supplied.

[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never in the 'loop' " - DreX 2005
 
>I actually need 3 fields to be null in order for the table not to show rather than one.
null and empty are not exactly the same, but for existing form fields, empty will do fine. The three form fields should have their own distinct id, figuratively, field1_id, field2_id, field3_id. There is nothing magic about it.
[tt]
if (document.getElementById('field1_id').value=='' &&
document.getElementById('field2_id').value=='' &&
document.getElementById('field3_id').value=='') {
document.getElementById('ID_OF_TABLE').style.display='none';
}
[/tt]
But with your script, the id's name FIELDNAME seems to suggest you're using name attribute of form field elements. Be careful then, no. Assign to them distinct id attribute.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top