Results 1 to 3 of 3

Thread: Creating div's on the fly

  1. #1
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating div's on the fly

    Hi everyone!

    I'm trying to create a form that when a user enters text and presses the button the text appears on the page and the user is able to drag the text around. The code below which uses a Dynamic Drive drag script achieves this but I want to create a new div everytime the button is pressed so that the new text is separately draggable. Is this possible? My existing code (highlighted) just overtypes the previous text. I'd really appreciate any help.

    Code:
    <html>
    <head>
    
    <style type="text/css">
    .drag{
        position:relative;
    	cursor:move;
    	z-index: 100;
    } 
    </style>
    
    <script type="text/javascript">
    /***********************************************
    * Drag and Drop Script: © Dynamic Drive http://www.dynamicdrive.com
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/
    
    var dragobject={
    z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
    initialize:function(){
    document.onmousedown=this.drag
    document.onmouseup=function(){this.dragapproved=0}
    },
    drag:function(e){
    var evtobj=window.event? window.event : e
    this.targetobj=window.event? event.srcElement : e.target
    if (this.targetobj.className=="drag"){
    this.dragapproved=1
    if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
    if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
    this.offsetx=parseInt(this.targetobj.style.left)
    this.offsety=parseInt(this.targetobj.style.top)
    this.x=evtobj.clientX
    this.y=evtobj.clientY
    if (evtobj.preventDefault)
    evtobj.preventDefault()
    document.onmousemove=dragobject.moveit
    }
    },
    moveit:function(e){
    var evtobj=window.event? window.event : e
    if (this.dragapproved==1){
    this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
    this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
    return false
    }
    }
    }
    
    dragobject.initialize()
    </script>
    
    <script type="text/javascript">
    
    function sendtoDiv(){
    document.getElementById("divbox").innerHTML = document.getElementById("input").value;
    document.createElement('divbox'); document.body.appendChild();
    }
    
    </script>
    
    </head>
    <body>
    
    <input type="text" id="input">
    <input type="button" onClick="sendtoDiv();" value="Create">
    <div class="drag" id="divbox"></div>
    
    </body>
    </html>

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You mean something like this:
    Code:
    function sendtoDiv(){
    var k=document.createElement('div'); 
    var val=document.getElementById("input").value;
    k.innerHTML=val;
    k.setAttribute('class','drag');
    document.body.appendChild(k);
    }
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Yes! That's awesome rangana! Thanks so much! In your example I don't even really need the <div class="drag" id="divbox"></div> hardwritten now. Thanks again!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •