undefined getElementById

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    undefined getElementById

    Why
    var offer=document. getElementById( "editor_display ing_text").valu e;
    returns undefined?
    Thank You
  • tswaters
    New Member
    • Feb 2007
    • 19

    #2
    Originally posted by oll3i
    Why
    var offer=document. getElementById( "editor_display ing_text").valu e;
    returns undefined?
    Thank You
    it's really hard to tell without knowledge of what "editor_display ing_text" is.

    I'm going to go on a limb and say not all elements have a property called "value"... in fact, most do not. Unless "editor_display ing_text" is an "OBJECT", "PARAM", "LI", "INPUT" or "BUTTON", value will return undefined.

    Read the DTD: http://www.w3.org/TR/html/DTD/xhtml1-transitional.dt d, do a CTRL-F for "value" and see how often it shows up.... not very often.

    You may want to check out the "childNodes " array for a list of dom elements within the element -- or, if you like the cheap & easy way out, innerHTML.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      It's an editable div which id is editor_displayi ng_text
      Thank You
      Last edited by oll3i; Feb 14 '08, 11:46 AM. Reason: typo

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You'll have to show your code. Where do you define it within the page?

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          Code:
           function makeBold(txt){
          
           var offer=document.getElementById("editor_displaying_text").value;
           
          
           offer=offer+'\n' ;
           
           offer=offer.replace(txt,'<b>'+ txt +'</b>');
           
           document.getElementById("editor_displaying_text").value = offer;
           }

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by oll3i
            Code:
             function makeBold(txt){
            
             var offer=document.getElementById("editor_displaying_text").value;
             
            
             offer=offer+'\n' ;
             
             offer=offer.replace(txt,'<b>'+ txt +'</b>');
             
             document.getElementById("editor_displaying_text").value = offer;
             }
            That code still does not show the <div>

            Ronald

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              ...and the code for the "editor"?

              Edit: Ah, ronverdonk replied in the meantime!

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by acoder
                ...and the code for the "editor"?

                Edit: Ah, ronverdonk replied in the meantime!
                Won't do it again.

                Ronald

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by ronverdonk
                  Won't do it again.

                  Ronald
                  No, I didn't mean you did something wrong (in fact, quite the opposite)! I was in the middle of replying and by the time I replied, you'd already done so. I just added an edit in case it looked like a duplicate response.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by acoder
                    No, I didn't mean you did something wrong (in fact, quite the opposite)! I was in the middle of replying and by the time I replied, you'd already done so. I just added an edit in case it looked like a duplicate response.
                    I was just kidding. Nobody prevents me from meddling in other forums/fora/forae?

                    Ronald

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #11
                      Code:
                       <script language = "JavaScript">
                       
                       function getSelectedText(){
                       var txt = '';
                      if (document.getSelection) txt = document.getSelection();
                      else if (document.selection) txt = document.selection.createRange().text;
                      
                      return txt;
                      
                       }
                       
                       
                       function makeBold(txt){
                      
                       var offer=document.getElementById("editor_displaying_text").value;
                       
                       document.write(offer);
                       offer=offer+'\n' ;
                       
                       offer=offer.replace(txt,'<b>'+ txt +'</b>');
                       
                       document.getElementById("editor_displaying_text").value = offer;
                       }
                       
                       
                       </script>
                      </HEAD>
                      <BODY>
                      <form name="editor">
                      <input type="button" name="bold"  class="editor_bold" onClick="makeBold(getSelectedText())">
                      <input type="button" name="unordered_list" class="editor_unordered_list">
                      <input type="button" name="anchor" class="editor_anchor">
                      
                      </form>
                      
                      <div id="editor_displaying_text" contenteditable="true" indicateeditable="true"></div>
                      it is just my attempt to write a simple editor
                      Thank You

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by ronverdonk
                        I was just kidding. Nobody prevents me from meddling in other forums/fora/forae?

                        Ronald
                        Lol, you never know these days how people are easily offended.

                        Oll3i, as tswaters mentioned earlier, the div tag doesn't have a value attribute. Either use innerHTML or the DOM methods.

                        One other thing, you can't use document.write after the document has loaded.

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #13
                          thank You that works I'm happy :)

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Glad to hear it!

                            Comment

                            Working...