Web Analytics Made Easy -
StatCounter multiple getElementById - CodingForum

Announcement

Collapse
No announcement yet.

multiple getElementById

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple getElementById

    Hey Guys,

    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 Head in

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

    Thanks in advance for any help
    Last edited by bleak7; Apr 3, 2009, 10:54 PM.

  • #2
    Ummm...why not just do three separate assignments.

    It's not elegant, perhaps, but it works and is fast and clean:
    Code:
    if (document.getElementById('FIELDNAME').value=='') 
    {
            document.getElementById('ID1').style.display='none';
            document.getElementById('ID2').style.display='none';
            document.getElementById('ID3').style.display='none';
    }
    Naturally use your own choices for the three ID's.

    If you showed the HTML with the stuff to be hidden, we might be able to suggest an alternative. But, really, there's nothing wrong with the above.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      Originally posted by Old Pedant View Post
      Ummm...why not just do three separate assignments.

      It's not elegant, perhaps, but it works and is fast and clean:
      Code:
      if (document.getElementById('FIELDNAME').value=='') 
      {
              document.getElementById('ID1').style.display='none';
              document.getElementById('ID2').style.display='none';
              document.getElementById('ID3').style.display='none';
      }
      Naturally use your own choices for the three ID's.

      If you showed the HTML with the stuff to be hidden, we might be able to suggest an alternative. But, really, there's nothing wrong with the above.
      Cheeers for that.

      It's actually the 'FIELDNAME' name i need multiple's of not the 'ID1' etc.

      So multiple selections in the IF statement.

      So more like

      Code:
      	if (document.getElementById('FIELDNAME1').value==''
      		document.getElementById('FIELDNAME2').value==''
      		document.getElementById('FIELDNAME3').value=='') {
      		document.getElementById('ID_OF_TABLE').style.display='none';
      
      	}
      But that is throwing up errors.

      So if FEILDNAME1,FEILDNAME2 and (not or) FEILDNAME3 are null then ID_OF_TABLE will be hidden.

      Thanks for your help mate. I'm fairly new two JS and do endevour to figure things out myself but this one is giveing me grief.

      Comment


      • #4
        Oh...you just need to use "AND". But you have to use the JavaScript "AND". Thus:

        Code:
        if (    document.getElementById('FIELDNAME1').value==''
             && document.getElementById('FIELDNAME2').value==''
             && document.getElementById('FIELDNAME3').value=='') 
        {
            document.getElementById('ID_OF_TABLE').style.display='none';
        }
        && is the "logical and" of JavaScript (and Java and C and C++ and PHP and C# and...).
        Be yourself. No one else is as qualified.

        Comment

        Working...
        X