SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    access html elements in internet explorer

    I have a problem and donīt know how to fix it, I have this code

    Code JavaScript:
    var apply=document.getElementById("regulartextfields");				
    var counterrors=0;
    if (apply.addEventListener)
    {
    apply.addEventListener("change", checkfield_1, false);
    }
    else if (apply.attachEvent)
    {
    apply.attachEvent("onchange", checkfield_1);
    }

    which works perfectly fine in both internet explorer and firefox for the following case

    Code HTML4Strict:
    <input type="text" size="15" id="regulartextfields">

    But only works in firefox in this case

    Code HTML4Strict:
    <div id="regulartextfields">
    <input type="text" size="15"/>
    <input type="text" size="15"/>
    <input type="text" size="15"/>
    <input type="text" size="15"/>
    </div>

    In the last one firefox will attach an event to each field but it will stop working completely in IE, can someone tell me how I should go about attaching an event to multiple elements in IE?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <div id="regulartextfields">
    <input type="text" size="15"/>
    <input type="text" size="15"/>
    <input type="text" size="15"/>
    <input type="text" size="15"/>
    </div>
    
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    function checkfield_1(){
     alert('change');
    }
    
    function Init(){
     var apply=document.getElementById("regulartextfields");
     apply=apply.nodeName.toUpperCase()=='INPUT'?[apply]:apply.getElementsByTagName('INPUT');
     var counterrors=0;
     for (var z0=0;z0<apply.length;z0++){
      if (apply[z0].addEventListener){
       apply[z0].addEventListener("change", checkfield_1, false);
      }
      else if (apply[z0].attachEvent){
       apply[z0].attachEvent("onchange", checkfield_1);
      }
     }
    }
    
    Init();
    /*]]>*/
    </script>
    </body>
    
    </html>

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your input, but since I am quite new to js I did not understand very well everything, could you please explain this line?
    Code JavaScript:
    apply=apply.nodeName.toUpperCase()=='INPUT'?[apply]:apply.getElementsByTagName('INPUT');
    thankyou

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your require ment is for an individual INPUT or inputs nested in a DIV
    so for an individual INPUT the line creats a arry of 1 INPUT
    and for inputs nested in a DIV creats a arry of all nested INPUTs


Bookmarks

Posting Permissions

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