WEB javascript: accessing named form elements in IE

Discussion in 'OT Technology' started by Limp_Brisket, Apr 29, 2008.

  1. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    so i use firebug to debug my javascript and it displays the DOM, so i used document.forms[0].elements['myNamedelement'].value to access it and it works great, but i doesn't work in IE. what would i use to make IE happy?

    and speaking of which, is there a good website that lists the DOM for major browsers?
     
  2. have you tried getelementbyid?
     
  3. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    i don't have id's, and i'm accessing them en masse which means giving them all id's would be a pain. right now i have it loop through all the elements and compare the name value i have with the element names and then update them if they are the same.

    that works in IE but seems kind inefficient considering i already have their names and could access them directly if IE wouldn't be gay.
     
  4. wiredup

    wiredup Guest

    Are you generating these elements after the page loads? Why not put them into an array then access them that way?
     
  5. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,918
    Likes Received:
    10
    Location:
    Los Angeles
    Then you would need to use, in conjunction, both the getElementsById, getElementsByTagName, and the getAttribute object respectively. You would be able to select the names of each form element utilizing getAttribute. From here you can cycle through each tag name within your form element id and get/alter each form name.

    Example
    Code:
    var formroot = document.getElementById("form_name");
    var formelement = formroot.getElementsByTagName("input");
    for (var i=0; i<formelement.length; i++) {
      var formelementnames = formelement[i].getAttribute("name");
      alert(formelementnames);
    }
    
    Code isn't tested. Just an idea on how to implement what you're trying to do. You should be able to utilize this into what you need.
     
  6. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    well, im getting the names out of an array where i had stored them previously. so it there was some way to interpolate the var as 'thename' then yes, i could do it that way. although i only know how to do that in php and perl :o
     
  7. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    that's pretty much what i'm already doing, looping through and comparing the name to see if it's the right input, then altering it. it just seems inefficient considering i already have an array filled with all the form input names that i want to alter so i tried just looping through my array like

    for(var i in myArray){
    document.forms[0].elements.value = blah;
    }

    that's what works in firefox but not IE. in IE i'm doing something more like

    for(var i =0; i<document.forms[0].elements.length;i++){
    for(var j in myArray){
    if(j == document.forms[0].elements.name){
    blah blah edit here;
    }
    }
    }

    i guess instead of looping twice i could replace the second loop with something like if(myArray[document.forms[0].elements[j]] != "") or something although both of these soltuions are still worse tahn what works in firefox, just accessing the element by its name which is in the array.
     
  8. :rofl: i heard that same exact quote at school like maybe 3 or 4 times this week.
     

Share This Page