+ Reply to Thread
Results 1 to 8 of 8

Thread: javascript appendChild

  1. #1
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    javascript appendChild

    lil snippet from one of my functions in a project I'm workin on. now see, the first part here works, but the second part won't append msgArea to area, but it will append it to body. I have no idea what the problem is, and firefox's error console doesn't tell me a thing. I know I can append to another div, cause I did it at other times in the code.


    Code:
        var body = document.getElementsByTagName("body"); 
        var area = document.createElement("div"); 
        body = body[0];
        body.appendChild(area); 
        this.cardAreas[this.cardAreas.length] = area; 
        area.setAttribute("id", name); 
        area.style.position = "absolute"; 
        area.style.width = "310px"; 
        area.style.height = "200px"; 
        area.style.border = "1px solid #000000"; 
        area.style.textAlign = "center";
        area.style.font = "10pt normal sans-serif"; 
        area.active = true; 
        area.bet = 0; 
     
        //create an area for messages 
        area.msgArea = document.createElement("div");
        area.appendChild(area.msgArea); 
        area.msgArea.style.zIndex = "125"; 
        area.msgArea.style.position = "absolute"; 
        area.msgArea.style.top = "50px"; 
        area.msgArea.style.left = "0px"; 
        area.msgArea.style.width = "310px";
        area.msgArea.style.font = "30pt thick sans-serif"; 
        area.msgArea.style.textAlign = "center";
        area.msgArea.innerHTML = "pie";
    odds are it's something really stupid that I'm missing here...

  2. #2
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: javascript appendChild

    Try this and tell me if it works, otherwise can you post a link to the entire page?
    Code:
    //create an area for messages 
        var msgArea = document.createElement("div");
        area.appendChild(msgArea); 
        msgArea.style.zIndex = "125"; 
        msgArea.style.position = "absolute"; 
        msgArea.style.top = "50px"; 
        msgArea.style.left = "0px"; 
        msgArea.style.width = "310px";
        msgArea.style.font = "30pt thick sans-serif"; 
        msgArea.style.textAlign = "center";
        msgArea.innerHTML = "pie";
    Even though area is an object, I don't believe you can create class variables for it, or if you can you would have to use the prototype keyword: area.prototype.msgArea = document.createElement("div"). As an HTML element though, I can't be sure.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: javascript appendChild

    @OP: The fragment you posted works for me. Would you provide a link to a live minimal test case?

    Note that you can get the body element with document.body; no need to call 'getElementsByName'.

    Quote Originally Posted by Twinkie View Post
    Even though area is an object, I don't believe you can create class variables for it, or if you can you would have to use the prototype keyword: area.prototype.msgArea = document.createElement("div"). As an HTML element though, I can't be sure.
    area is an object like any other. You can use area.constructor.prototype to add "class" properties (not that JS has classes) and you can add object properties as Ixonal does.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: javascript appendChild

    well, I uploaded the whole thing, but.... I dunno if it's just my machine, but only a few kanji (I have japanese font support) are showing up, and all of the source looks screwy.... doesn't look like that on my system O.o

    http://ixonal.elementfx.com/projects...k/game2.2.html


    edit: btw, the offending code is near line 500 or so
    Last edited by Ixonal; 07-25-2009 at 12:47 AM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: javascript appendChild

    The kanji is preventing the JS from being executed, so I can't evaluate what else is going wrong. The document is also missing a <head> element and a closing tag for the <script>. What did you use to u/l the page?

    Rather than posting a page with all your code, write a minimal test case and post a link to a live copy.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: javascript appendChild

    first, there is a head and closing script tag, they're just absorbed by the kanji I think.

    second, I used gftp to upload the file

    third, I already did a test case, in that I've done that operation several times over and never had this issue. all of the cards in the deck object have other divs inserted into them.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: javascript appendChild

    Quote Originally Posted by Ixonal View Post
    first, there is a head and closing script tag, they're just absorbed by the kanji I think.
    They're sort of there. Some newline characters (looks like every one after the first few) are preceded by an extra null character, which throws off decoding UTF-16.

    Quote Originally Posted by Ixonal View Post
    second, I used gftp to upload the file
    Hmm... You could try transfering in binary mode if you used ASCII mode before, but I doubt that'll fix the problem. You could also try saving the original file as ASCII rather than UTF-16 using whatever editor you used to write it.

    Quote Originally Posted by Ixonal View Post
    third, I already did a test case, in that I've done that operation several times over and never had this issue. all of the cards in the deck object have other divs inserted into them.
    You misunderstand. A minimal test case is the smallest possible example you can come up with that illustrates your problem. See the link in my last post.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    Ixonal is offline x10Hosting Member Ixonal is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    29

    Re: javascript appendChild

    I figured it out! apparently it won't show up if you add the msgArea to area before altering area. it works when I make and add msgArea last when making the area object

+ Reply to Thread

Similar Threads

  1. Replies: 0
    Last Post: 08-17-2008, 01:46 AM
  2. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  3. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  4. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers