IE6 won't hide dynamically created span elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicholas Couch

    #1

    IE6 won't hide dynamically created span elements

    I have a little form with a couple of dynamically generated list
    boxes. When the user makes a selection from the first box, the second
    box is refreshed. When they make a selection from the second box, I
    concatenate the selections from the two boxes and add the string to a
    list at the top of the form, using createElement and appendChild. The
    list is actually a bunch of span elements contained within a div. Each
    span element includes a small graphic of a red X, which the user can
    click to delete that item from the list.

    All of this works just fine in Mozilla and Opera, but - wait for it -
    the delete functionality fails in IE, throwing an "Object Required"
    error on this line:

    this.style = document.getEle mentById(nm).st yle;

    where nm is the ID of the span element I'm trying to hide, by setting
    it's display property to "none."

    What's really interesting is the fact that I have one or more span
    elements in that div when the form first loads, and IE will hide these
    HTML-based elements without complaining. It's only the spans that I
    generate on the fly that it has a problem hiding.

    Must be something wrong with the way I'm creating those dynamic spans,
    you say? Maybe so, but IE does display them okay, and the code all
    works great in the other two browsers. Now I'm no javascript veteran,
    by any means, but this appears to me to be a bug in IE. What I asking
    here is whether anybody knows of a workaround for this, or maybe a
    better way of accomplishing what I'm trying to do.

    Thanks in advance,
    Blake Couch
    Javascript Noob
  • john henry bonham

    #2
    Re: IE6 won't hide dynamically created span elements

    Nicholas Couch wrote:

    [color=blue]
    > this.style = document.getEle mentById(nm).st yle;[/color]

    That should be 'nm' ...

    this.style = document.getEle mentById('nm'). style;

    I can only guess that's your problem without actually looking at the code.

    Comment

    • Fred Oz

      #3
      Re: IE6 won't hide dynamically created span elements

      Nicholas Couch wrote:
      [snip][color=blue]
      > All of this works just fine in Mozilla and Opera, but - wait for it -
      > the delete functionality fails in IE, throwing an "Object Required"
      > error on this line:
      >
      > this.style = document.getEle mentById(nm).st yle;
      >
      > where nm is the ID of the span element I'm trying to hide, by setting
      > it's display property to "none."[/color]
      [snip]

      You could try putting your elements inside <div> rather than
      <span> - I think the effect is the same and you have much more
      functionality available from divs. Try something like:

      divRef.style.di splay='none';

      where divRef is a reference to the div you want to hide.

      I tried the above, it worked fine in Safari and IE 5.2 on
      Mac - no guarantee for IE on Windows but I have done a similar
      thing for Windows using divs and it works fine.

      Cheers, Rob

      Comment

      • Richard Cornford

        #4
        Re: IE6 won't hide dynamically created span elements

        Nicholas Couch wrote:
        <snip>[color=blue]
        > Must be something wrong with the way I'm creating those
        > dynamic spans, you say?[/color]

        Yes.
        [color=blue]
        > Maybe so, but IE does display them okay, and the
        > code all works great in the other two browsers.[/color]

        So it is not 100% faulty.
        [color=blue]
        > Now I'm no javascript veteran, by any means,[/color]

        Obviously, as it doesn't take much experience to work out that in order
        to debug code people need to be able to see it.
        [color=blue]
        > but this appears to me to be a bug in IE.[/color]

        What, a web browser with a bug? ;)
        [color=blue]
        > What I asking here is whether anybody knows of
        > a workaround for this,[/color]

        It probably doesn't need a workaround. Cross browser code would probably
        suffice.
        [color=blue]
        > or maybe a better way
        > of accomplishing what I'm trying to do.[/color]

        What you are trying to do (at least claming that you are trying to do)
        works successfully in IE browsers.

        Richard.


        Comment

        • Nicholas Couch

          #5
          Re: IE6 won't hide dynamically created span elements

          Fred Oz <ozfred@iinet.n et.auau> wrote in message news:<414aa137$ 0$24385$5a62ac2 2@per-qv1-newsreader-01.iinet.net.au >...[color=blue]
          > Nicholas Couch wrote:
          > [snip][color=green]
          > > All of this works just fine in Mozilla and Opera, but - wait for it -
          > > the delete functionality fails in IE, throwing an "Object Required"
          > > error on this line:
          > >
          > > this.style = document.getEle mentById(nm).st yle;
          > >
          > > where nm is the ID of the span element I'm trying to hide, by setting
          > > it's display property to "none."[/color]
          > [snip]
          >
          > You could try putting your elements inside <div> rather than
          > <span> - I think the effect is the same and you have much more
          > functionality available from divs. Try something like:
          >
          > divRef.style.di splay='none';
          >
          > where divRef is a reference to the div you want to hide.
          >
          > I tried the above, it worked fine in Safari and IE 5.2 on
          > Mac - no guarantee for IE on Windows but I have done a similar
          > thing for Windows using divs and it works fine.
          >
          > Cheers, Rob[/color]


          Thanks for the suggestion, but I've actually tried all sorts of
          combinations of elements - spans inside a div, divs inside a span,
          divs inside another div, paragraphs, you name it. The spans are what I
          really want to use, for cosmetic reasons.

          It always comes back to the same thing: it's the dynamically generated
          elements that IE chokes on, no matter what type they are. It's like
          those objects are not getting registered somehow, even though they
          display okay.

          Thanks again,
          BC

          Comment

          • Andrew Thompson

            #6
            Re: IE6 won't hide dynamically created span elements

            On 17 Sep 2004 08:14:58 -0700, Nicholas Couch wrote:
            [color=blue]
            > ..I've actually tried all sorts of
            > combinations of elements - spans inside a div, divs inside a span,
            > divs inside another div, paragraphs, you name it.[/color]

            A process commonly referred to as
            'making it up as you go along'.

            The simple fact is that if you are writing
            invalid HTML, dynamically or otherwise, all
            bets are off as to how it will render.

            You should create a static HTML page of the
            basic element structure first, and confirm
            that it is valid HTML.
            <http://validator.w3.or g/>
            [color=blue]
            >..The spans are what I
            > really want to use, for cosmetic reasons.[/color]

            ???

            Do you want an element that spans from
            one side of the browser to the other?
            Use a <div>.

            Do you want an element that can be contained
            within a line (or lines) of text?
            Use a <span>.

            [ Your suggestion of 'spans indside divs'
            is invalid HTML. ]

            Once you're content is marked up with the
            appropriate HTML elements, you can use CSS
            to specify the look of each.

            HTH

            --
            Andrew Thompson
            http://www.PhySci.org/codes/ Web & IT Help
            http://www.PhySci.org/ Open-source software suite
            http://www.1point1C.org/ Science & Technology
            http://www.lensescapes.com/ Images that escape the mundane

            Comment

            • Nicholas Couch

              #7
              Re: IE6 won't hide dynamically created span elements

              Richard -

              I didn't post my code because it's pretty clear that IE, not my code, is
              the problem. I'm pretty sure that I am indeed using cross-browser DOM1
              code, and as I already said, it all works great in at least two other
              browsers. What I was hoping to find was a response from someone who has
              run up against the same bug in IE and found a workaround, but since you
              seem to be so keen on seeing the code, here it is:

              function positionChange( )
              {
              var b = document.form1. baseSelect;
              var posSel = document.form1. positionSelect;
              var base = b.options[b.selectedIndex].text
              var p = posSel.options[posSel.selected Index].text
              var v = posSel.options[posSel.selected Index].value;
              var newElem = "span";
              // here's where we create a new element under the "prefs" element
              // first we get a reference to prefs
              var pr = new getObj("prefs") ;
              // then we create a new element
              var newel = document.create Element(newElem );
              // finally, append the new element to the prefs element
              pr.obj.appendCh ild(newel);
              // ???
              newel.setAttrib ute("ID", "pref" + v);
              newel.setAttrib ute("style", "display:block; ");
              // create an anchor element for the new element
              var an = document.create Element("a");
              // append the anchor to the new element
              newel.appendChi ld(an);
              an.setAttribute ("href", "javascript:dro p_pref(\"pref" + v + "\")");
              an.setAttribute ("title", "delete");
              // create a new IMG element to go with the anchor
              var newimg = document.create Element("img");
              // append the image to the anchor
              an.appendChild( newimg);
              newimg.setAttri bute("src", "/common/images/delete_small.gi f");
              newimg.setAttri bute("border", "0");
              newimg.setAttri bute("alt", "delete");
              // create a TextNode element for the new element and append it
              var txt = document.create TextNode(" " + base + " - " + p);
              newel.appendChi ld(txt);
              // create a line break and append that
              var br = document.create Element("br");
              newel.appendChi ld(br);
              }

              function drop_pref(id)
              {
              var x = new getObj(id);
              // alert("display = " + x.style.display );
              x.style.display = 'none';
              }

              function getObj(nm)
              {
              if (document.getEl ementById)
              {
              this.obj = document.getEle mentById(nm);
              this.style = document.getEle mentById(nm).st yle;
              }
              else if (document.layer s)
              {
              this.obj = document.layers (nm);
              this.style = document.layers (nm);
              // "old" Netscape
              }
              else if (document.all)
              {
              this.obj = document.all(nm );
              this.style = document.all(nm ).style;
              // old IE
              }
              }

              I appreciate any insight you can give on this problem.

              Thanks,
              BC


              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Andrew Thompson

                #8
                Re: IE6 won't hide dynamically created span elements

                On Fri, 17 Sep 2004 15:35:38 GMT, Andrew Thompson wrote:
                [color=blue]
                > [ Your suggestion of 'spans indside divs'
                > is invalid HTML. ][/color]

                ....errr, whoops! Should have validated that.

                It is 'divs inside spans' that is invalid.

                Comment

                • Richard Cornford

                  #9
                  Re: IE6 won't hide dynamically created span elements

                  Nicholas Couch wrote:[color=blue]
                  > Richard -
                  >
                  > I didn't post my code because it's pretty clear that IE,
                  > not my code, is the problem.[/color]

                  And as you cannot change IE you therefor could not solve your problem by
                  changing your code.
                  [color=blue]
                  > I'm pretty sure that I am indeed using cross-browser
                  > DOM1 code,[/color]

                  DOM 1 code is not necessarily cross-browser (even cross-DOM 1 supporting
                  browsers).
                  [color=blue]
                  > and as I already said, it all works great in at least two
                  > other browsers. What I was hoping to find was a response
                  > from someone who has run up against the same bug in IE
                  > and found a workaround,[/color]

                  Without being able to see the code it is not possible to know which
                  bug/feature of IE you have run up against (all browsers have bugs and
                  features, usually lot of them).
                  [color=blue]
                  > but since you seem to be so keen on seeing the code, here
                  > it is:
                  >[/color]
                  <snip>[color=blue]
                  > newel.setAttrib ute("ID", "pref" + v);[/color]
                  ^^
                  Code == quick and painless answer!

                  When creating elements dynamically, instead of using setAttribute,to set
                  HTMLElement properties listed in the HTML DOM it is advisable to set the
                  corresponding object properties directly. In this case the - id -
                  property of the new span element:-

                  newel.id = ("pref" + v);

                  - the resulting code works with all HTML DOM browsers.

                  If you insist on using setAttribute then it is necessary to use
                  attribute names in the form that IE understands them, thus exactly the
                  same mixed case strings as are found as named properties of IE's
                  attributes collection. In this case IE will act on the setting of an
                  "id" attribute if that attribute name is _all_ lowercase_. (But it is
                  easier to set the properties directly.)

                  <snip>[color=blue]
                  > newel.appendChi ld(an);
                  > an.setAttribute ("href",
                  > "javascript:dro p_pref(\"pref" + v + "\")");[/color]
                  <snip>

                  Javascript pseudo-protocol HREFs are known troublemakers and should
                  never be used in cross browser code. Instead, at this point you should
                  assign a harmless - href - property (as it will never be acted upon '#'
                  would do) and assign a navigation-cancelling onclick handler to the -
                  onclick - property of the A element. Then you no longer need an ID
                  attribute anyway as such an onclick handler can eliminate its containing
                  span from the document with:-

                  this.parentNode .parentNode.rem oveChild(this.p arentNode);

                  As could a similar event handler on <BUTTON> or <INPUT type="image">
                  elements in the same context.

                  Richard.


                  Comment

                  • Nicholas Couch

                    #10
                    Re: IE6 won't hide dynamically created span elements

                    Well, Richard, you're absolutely right - I should have included my code
                    in my first post. Changing the setAttribute calls to setting properties
                    directly solved the problem. On the other hand, if IE didn't treat
                    setAttribute in a non-standard way, I wouldn't have had this problem in
                    the first place. I read in Danny Goodman's book that appendChild,
                    setAttribute and the like are DOM1 _and_ supported by IE as of version
                    5. If I had read closer, I would have seen his discussion of the way IE
                    handles setAttribute. It supports those calls alright, but in its own
                    peculiar way.

                    One thing still puzzles me. When I try to set the display style of my
                    new span with

                    newel.style = "display:block; ";

                    IE throws a "member not found" error. At first I simply commented this
                    line, and all was well. Then I changed it to read

                    newel.style.dis play = "block";

                    and that worked too, so I kept it. Now presumably the problem I was
                    having before had to do with IE not finding the span object in its
                    collection, or perhaps more precisely, not finding the style attribute
                    of the span object. So does IE create the necessary style attribute if I
                    fail to specify it myself, but not create it if I specify it in a way
                    that doesn't conform to its interpretation of setAttribute? More
                    importantly, does anybody care?

                    Thanks,
                    BC


                    *** Sent via Developersdex http://www.developersdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    • Richard Cornford

                      #11
                      Re: IE6 won't hide dynamically created span elements

                      Nicholas Couch wrote:[color=blue]
                      > Well, Richard, you're absolutely right - I should
                      > have included my code in my first post.[/color]

                      Yes you should, you would have had fixed code at least 4 hours sooner.
                      As should all the other individuals posting questions to this group who
                      don't know enough to appreciate that there are 50-odd ways of screwing
                      most things up and that it is impossible to tell which actually applies
                      without seeing the code and its context. (While we can all guess, and
                      even guess correctly sometimes, it is frustrating to be being
                      continually asked to guess when code is all that is needed for
                      certantly.)

                      <snip>[color=blue]
                      > ... . I read in Danny Goodman's book ...[/color]
                      <snip>

                      Danny Goodman's books are out of date and teach practices that are
                      positively harmful for cross-browser scripting.

                      <snip>[color=blue]
                      > One thing still puzzles me. When I try to set the display
                      > style of my new span with
                      >
                      > newel.style = "display:block; ";[/color]

                      The reason that I specifically stated that the properties that should be
                      defined directly on the element instead of using setAttribute are the
                      HTML attribute-corresponding properties defined in the _HTML_ DOM
                      specification was that other attributes are handled differently. The -
                      style - property of a DOM element (that implements the
                      ElementCSSInlin eStyle interface) refers to an object implementing the
                      CSSStyleDeclara tion interface from the W3C CSS DOM specification. Where
                      it is defined as a read-only property (the - style - property of the
                      element, not the CSSStyleDeclara tion interface's properties).

                      The ElementCSSInlin eStyle interface is formalisation of behaviour
                      originating in IE 4 and well supported in modern browsers. When an
                      element is created it already has a - style - property that refers to an
                      object, so it is best to set the properties of that object to the
                      desired values (or to set the - className - property of the element
                      itself, to have it acquire as set of style properties defined in CSS
                      elsewhere).
                      [color=blue]
                      > IE throws a "member not found" error. At first I simply commented
                      > this line, and all was well. Then I changed it to read
                      >
                      > newel.style.dis play = "block";
                      >
                      > and that worked too, so I kept it.[/color]

                      Yes that works, but it is a bit illogical to create a SPAN element (the
                      semantically neutral inline element intended for little more than the
                      application of CSS) and then give it a "block" display property, when
                      you could create a DIV element (the semantically neutral block element)
                      and just let its display property default to "block" as normal.
                      [color=blue]
                      > Now presumably the problem I was having before
                      > had to do with IE not finding the span object in its
                      > collection, or perhaps more precisely, not finding[/color]
                      [color=blue]
                      > the style attribute of the span object. So does
                      > IE create the necessary style attribute if I fail
                      > to specify it myself,[/color]

                      Any created element (at least on modern browsers) will have a - style -
                      property from the moment of its creation, that represents the current
                      state of its inline style, as an object with named properties.
                      [color=blue]
                      > but not create it if I specify it in a way that doesn't
                      > conform to its interpretation of setAttribute? More
                      > importantly, does anybody care?[/color]

                      It is normal to set the properties related to inline style on the object
                      provided not through the use of setAttribute('s tyle' ,' ... ');

                      Richard.


                      Comment

                      • Nicholas Couch

                        #12
                        Re: IE6 won't hide dynamically created span elements

                        Richard Cornford wrote:[color=blue]
                        >
                        > <snip>
                        >[color=green]
                        >>... . I read in Danny Goodman's book ...[/color]
                        >
                        > <snip>
                        >
                        > Danny Goodman's books are out of date and teach practices that are
                        > positively harmful for cross-browser scripting.
                        >
                        > <snip>
                        >[/color]

                        Virtually any book in the computing field is likely to be out of date
                        shortly after publication. I have the second edition of "Dynamic HTML,
                        the Definitive Reference," published in 2002. I gather you would not
                        consider it definitive, but is it really that out of date only two years
                        later? What would be a better resource for a confessed javascript noob?

                        BC





                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        • Michael Winter

                          #13
                          Re: IE6 won't hide dynamically created span elements

                          On 17 Sep 2004 20:41:13 GMT, Nicholas Couch <blake@thecouch es.net> wrote:

                          [snip]
                          [color=blue]
                          > Virtually any book in the computing field is likely to be out of date
                          > shortly after publication. I have the second edition of "Dynamic HTML,
                          > the Definitive Reference," published in 2002. I gather you would not
                          > consider it definitive, but is it really that out of date only two years
                          > later? What would be a better resource for a confessed javascript noob?[/color]

                          It's not that Javascript books are out-of-date, but that they're simply
                          bad in the first place. As Richard tends to point out in discussions such
                          as this, the majority of book and Web authors don't understand the
                          complexities of scripting[1], and the Web is worse-off for it. As such,
                          there tends to be few resources that can be recommended, aside from a few
                          choice articles on topics like the evils of browser detection.

                          Personally, I can do little more than recommend reading the FAQ and it
                          links, and continuing to read and ask questions in this group. It's not
                          quite the self-enabling answer you were probably looking for, but I don't
                          think there's much that can be about that with the current state of things.

                          Mike

                          <URL:http://jibbering.com/faq/>


                          [1] I'd like to point out that I don't claim to, either. It's why I
                          participate in this, and other, authoring groups. It's amazing to realise
                          just how clueless one can be. :D

                          --
                          Michael Winter
                          Replace ".invalid" with ".uk" to reply by e-mail.

                          Comment

                          • Grant Wagner

                            #14
                            Re: IE6 won't hide dynamically created span elements

                            Michael Winter wrote:
                            [color=blue]
                            > As Richard tends to point out in discussions such
                            > as this, the majority of book and Web authors don't understand the
                            > complexities of scripting[1], and the Web is worse-off for it.
                            >
                            > [1] I'd like to point out that I don't claim to, either. It's why I
                            > participate in this, and other, authoring groups. It's amazing to realise
                            > just how clueless one can be. :D[/color]

                            The complexities of scripting are also the reason I hear "JavaScript sucks" so
                            much. And I don't just hear it from developers, I also hear it from end-users
                            who fail to understand it is not the technology that "sucks", but the
                            particular style of scripting on the sites they are visiting. This badly
                            written script is caused by the exact thing you mentioned, Web authors not
                            understanding the complexities of the environment they are developing for.

                            When they are approached and informed that something they have authored is not
                            working on some combination of OS, browser, firewall and/or popup-blocking
                            software, their first reaction is... you guessed it... "JavaScript sucks".
                            Well, their first reaction might be "Windows sucks" or "IE sucks", or "Mac
                            sucks" or "Mozilla sucks", but ultimately it comes down to blaming something
                            other than their inability to understand that they do not control the
                            environment in which their code is running, and as a result, extra care must be
                            taken to ensure that when (not if) the code fails, it does so in a way that
                            will not leave the visitor's browser crippled.

                            As you said, I am still learning as well. I am lucky enough to do most of my
                            work for an Intranet where I support only(!) Netscape 4.78, IE 5.5+, recent
                            Gecko-based browsers and Opera 7. And I have the option of insisting that the
                            user upgrade to a more recent Gecko variation or Opera 7 if they are running an
                            older version which does not support some functionality which does not work
                            properly.

                            And even with this "controlled " environment, Windows XP Service Pack 2 revealed
                            some weakly written client-side JavaScript which caused problems and had to be
                            fixed.

                            When people tell me writing client-side JavaScript isn't "real" programming, I
                            just smile and resort to "if you say so". Then wait for them to have their
                            first experience with "why doesn't formName.submit (); work in Mozilla!". :)

                            --
                            Grant Wagner <gwagner@agrico reunited.com>
                            comp.lang.javas cript FAQ - http://jibbering.com/faq

                            Comment

                            • Richard Cornford

                              #15
                              Re: IE6 won't hide dynamically created span elements

                              Grant Wagner wrote:[color=blue]
                              > Michael Winter wrote:[color=green]
                              >> As Richard tends to point out ...[/color][/color]
                              <snip>[color=blue][color=green]
                              >> ... . It's amazing to realise
                              >> just how clueless one can be. :D[/color][/color]

                              But very essential in any effort to become non-clueless.

                              <snip>[color=blue]
                              > When people tell me writing client-side JavaScript isn't
                              > "real" programming, I just smile and resort to "if you say
                              > so". ...[/color]
                              <snip>

                              Earlier today I noticed a job advert:-

                              <URL: http://www.cwjobs.co.uk/JS/JobDetail...JobID=12451782 >
                              (That is unlikely to be a stable URL)

                              Containing the line: "You will need a solid experience of Web
                              Development generating HTML, Javascript, CSS and Graphics, ideally with
                              some programming.". Obviously written by someone who actually believes
                              they can hire someone to write javascript who doesn't necessarily know
                              anything about programming. I can imagine the outcome :-(

                              Richard.


                              Comment

                              Working...