Barry Watts
Barry Watts

Reputation: 794

jquery dynamic positioning

I have a dynamic textarea nested within a div. The div is resizable and draggable. I have created a delete icon for the div but want the delete icon to be in the NE corner of the div and move when the div is resized.

I have created the div, textarea and icon with this code

    function NewTextArea(id)
    {   

    id=id+i;
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', id);
    newdiv.setAttribute('class', 'dragbox');
    newdiv.setAttribute('iterate',i);
    newdiv.style.position = "relative";
    newdiv.style.top = p;
    newdiv.style.left = p;
    newdiv.style.cursor='move';
    newdiv.innerHTML = "</div><br><textarea id='"+i +"'  onDblClick='editor1("+i+")' name='textarea["+i +"]' class='textarea1' style='position:absolute; top:10px;left:0px;overflow-y: auto;background-color:transparent;border: 2px dashed #000; '>some text here"+i+"</textarea>";
    newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='"+i+"' name='id["+i+"]'><br><input name='box_type["+i+"]' type='hidden' value='text'/>"; 
    newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='300' name='width["+i+"]' id='width"+i+"'><br><input type='hidden' value='300' name='height["+i+"]' id='height"+i+"'>";               
    newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='0' name='left["+i+"]' id='left"+i+"'><br><input type='hidden' value='0' name='top["+i+"]' id='top"+i+"'>"; 

    document.getElementById("frmMain").appendChild(newdiv); 

    var but=document.createElement('input');
    but.setAttribute('type','button');
    but.setAttribute('class','removebutton');       

    but.onclick=function(){
        if(confirm('Really delete?'))
        {
        document.getElementById("frmMain").removeChild(newdiv);
        document.getElementById(id).removeChild(but);
        document.getElementById(id).removeChild(newbr);
        }       
    }

    newbr=document.createElement('BR');
    document.getElementById(id).appendChild(newbr);
    document.getElementById(id).appendChild(but);



    $(function()
    {
        $("#"+i).resizable({autoHide:true})
        $("#"+id).hover(function () {
            $("#"+i).css('border', '2px dashed #000');         
        });
        $("#"+id).mouseleave(function () {
            $("#"+i).css('border', '0px dashed #000');         
        });
        $("#"+i).resizable(
            {
                stop: function(event, ui)
                {
                    var width = ui.size.width;
                    var height = ui.size.height;
                    // alert("width="+width+"height="+height);
                    ValProportions(width,height,ui.element.context.id);           
                }
            });

        $( "#"+id ).draggable(
            {
                stop: function(event, ui)
                {
                    Stoppos = $(this).position();
                    $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
                    // alert("left="+Stoppos.left+"top="+Stoppos.top);
                    ValPostion(Stoppos.left,Stoppos.top,$(this).attr('iterate'));   
                }
            }); 
        $("#"+i).draggable({handle:"#handle"}); 
    });  

    function ValProportions(defaultwidth, defaultheight,id)  { 
        $('#width'+id).val(defaultwidth);
        $('#height'+id).val(defaultheight);
    }

   function ValPostion(defaultleft,defaulttop,id)  {  
        $('#left'+id).val(defaultleft);
        $('#top'+id).val(defaulttop);
    }

    p=p+25;     
    i++;

Upvotes: 0

Views: 299

Answers (2)

snaplemouton
snaplemouton

Reputation: 1549

Use a style on your button.

float:right

Will put your button to the right of the div. Just make sure you got the button as the first element inside the div.

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

Add the styles

.dragbox {
    position: relative;
}

.removebutton {
    position: absolute;
    top: 5px;
    right: 5px;
}

Upvotes: 1

Related Questions