Need simple javascript help

Discussion in 'JavaScript' started by bikerjeg, Apr 11, 2009.

  1. #1
    I am trying to edit a javascript function that ads rounded corner images to divs but I am having trouble getting it to work properly. I looked through some javascript sources like java2s.com to figure out how insertBefore,firstChild, and appendChild work but it seems I am still not using these correctly since when I modify the code I end up breaking the script.

    I am new to javascript but I have programmed in java before so it is fairly similar. Here is the code I need help with.

    
    var id=myDiv;
    var el=document.getElementById(id);
    var d = document.creatElement("div");
    d.className="tshade";
    el.style.padding="0";
    el.insertBefore(d,el.firstChild);
    
    Code (markup):
    what's wrong with this?

    Basically what I want to do is once I get the div is add a div with two elements inside.
    So basically the html source should look like this once the javascript runs
    <div id=myDiv>
      <ELEMENT1>
        <ELEMENT2>
        </ELEMENT2>
      </ELEMENT1>
    </div>
    HTML:
     
    bikerjeg, Apr 11, 2009 IP
  2. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    in DOM, you can not set value to an className attribute directly, you must use setAttribute method, please view code in link below to understand it, or you can go JavaScriptKit.com for different DOM methods

    http://javascriptbank.com/forum/viewtopic.php?f=29&t=717


    var id=myDiv;
    var el=document.getElementById(id);
    var d = document.creatElement("div");
    [COLOR="Red"]d.setAttribute( "class", "tshade" );[/COLOR]
    el.style.padding="0";
    el.insertBefore(d,el.firstChild);
    Code (markup):
     
    JavaScriptBank.com, Apr 11, 2009 IP