Results 1 to 4 of 4

Thread: Adding new div

  1. #1
    Junior Member
    Join Date
    Sep 2005
    Answers
    19

    Adding new div

    How to add new div using javascriprt?

    Thanks in Advance
    Srikanth


  2. #2
    Moderator
    Join Date
    Oct 2005
    Answers
    305

    Re: Adding new div

    You use use
    HTMLElement.appendChild().

    alternatively you can also try
    document.createElement() in combination with document.insertBefore()

    Thanks
    Lokesh M.


  3. #3
    Expert Member
    Join Date
    Mar 2012
    Answers
    208

    Re: Adding new div

    The div tag is created dynamically by using document.createElement method of JavaScript.The object created by createElement method can access all the supported properties, functions and methods of document.documentElement method of JavaScript.
    Example
    Javascript Create Div Element Dynamically



  4. #4

    Re: Adding new div

    To add any element through JavaScript, you can use the method document.createElement("div"). And to set other attributes like "id" or "class" or to add text, this is the way to do it.

    Here div2 is being appended inside div1. Simply to add a div, remove the line "div1.appendChild(dElement);"

    HTML Code:
    <div id="div1" class="samp">
    
    <script type="text/javascript">
    
           var dElement = document.createElement("div");
    
                dElement.setAttribute("id","div2");
    
                dElement.setAttribute("class","samp2");
    
                dElement.innerHTML="this is div 2 ";  /* to add text to div2 */
    
                div1.appendChild(dElement);
    
    </script>
    
    </div>  <!-- div1 ending -->


    Last edited by Kishore Rajendra; 03-25-2014 at 05:18 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
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact