Nagri
Nagri

Reputation: 3136

alignment issue of div tag

I am trying to create a web page where on click of a button I can add div tags. What I thought to do was that I'll create two div tags within a single div so that over all presentation will be uniform and similar to a table having two columns and multiple rows and the first column contains only label's and second column will contain textbox.

Here is the JS file:

        var counter = 0;

function create_div(type){
        var dynDiv = document.createElement("div");

             dynDiv.id = "divid_"+counter;
             dynDiv.class="main";
             document.body.appendChild(dynDiv);
             question();
             if(type == 'ADDTEXTBOX'){
                ADDTEXTBOX();
             }

             counter=counter+1;
        }

    function question(){
        var question_div = document.createElement("div");
            question_div.class="question";
            question_div.id = "question_div_"+counter;
            var Question = prompt("Enter The Question here:", "");

            var node=document.createTextNode(Question);
            question_div.appendChild(node);
            var element=document.getElementById("divid_"+counter);   
            element.appendChild(question_div);
        }

    function ADDTEXTBOX(){
            var answer_div = document.createElement("div");
            answer_div.class="answer";
            answer_div.id = "answer_div_"+counter;
            var answer_tag = document.createElement("input");
                    answer_tag.id = "answer_tag_"+counter;
                    answer_tag.setAttribute("type", "text");
                    answer_tag.setAttribute("name", "textbox");
                        answer_div.appendChild(answer_tag);

            var element=document.getElementById("divid_"+counter);
            element.appendChild(answer_div);
        }

Here is the css file:

.question
{
    width: 40%;
    height: auto;
    float: left;
    display: inline-block; 
    text-align: justify;
    word-wrap:break-word;
}
.answer
{
    padding-left:10%;
    width: 40%;
    height: auto;
    float: left;
    overflow: auto;
    word-wrap:break-word;
}
.main
{   
    width: auto;
    background-color:gray;
    height: auto;
    overflow: auto;
    word-wrap:break-word;
}

My problem is that the code is working properly but both the divisions are not coming in a straight line. after the first div prints on the screen the second divisions comes in another line. How can I make both the div's come in the same line?

PS: should I stick with the current idea of using div or should I try some other approach? like tables?

Upvotes: 0

Views: 169

Answers (4)

scx
scx

Reputation: 2789

The reason its in diffrent lines lies in your JS code, try setting your class like following:

           //question_div.class="question";
        question_div.setAttribute("class", "question") ;  

and

            //answer_div.class="answer";
        answer_div.setAttribute("class", "answer"); 

and also this:

//dynDiv.class="main";
dynDiv.setAttribute("class", "main"); 

Your divs have not class attribute set properly. I recommend chrome in-built tools for developers or FireBug add-on if you use Firefox to check whether elements you built are like you design them to be. You may check code here: http://jsfiddle.net/Nnwbs/2/

      var counter = 0;

function create_div(type){
    var dynDiv = document.createElement("div");

         dynDiv.id = "divid_"+counter;
         //dynDiv.class="main";
         dynDiv.setAttribute("class", "main"); 
         document.body.appendChild(dynDiv);
         question();
         if(type == 'ADDTEXTBOX'){
            ADDTEXTBOX();
         }

         counter=counter+1;
    }

function question(){
    var question_div = document.createElement("div");
       //question_div.class="question";
        question_div.setAttribute("class", "question") ;  
        question_div.id = "question_div_"+counter;
        var Question = prompt("Enter The Question here:", "");

        var node=document.createTextNode(Question);
        question_div.appendChild(node);
        var element=document.getElementById("divid_"+counter);   
        element.appendChild(question_div);

    }

function ADDTEXTBOX(){
        var answer_div = document.createElement("div");
        //answer_div.class="answer";
        answer_div.setAttribute("class", "answer");            
        answer_div.id = "answer_div_"+counter;
        var answer_tag = document.createElement("input");
                answer_tag.id = "answer_tag_"+counter;
                answer_tag.setAttribute("type", "text");
                answer_tag.setAttribute("name", "textbox");
                    answer_div.appendChild(answer_tag);

        var element=document.getElementById("divid_"+counter);
        element.appendChild(answer_div);

}
create_div("ADDTEXTBOX");

​And about that aproach I mean div or tables, you are correct to use div, its generaly recommended to do so.

Also after you correct your JS code fix also a bit your css styles as you like.

Upvotes: 1

Vinod Vishwanath
Vinod Vishwanath

Reputation: 5891

It's simple. To line up both div's, give the position of of the two div's as display:inline-block;

display:inline-block;

Note: BOTH div's have to have this property for them to appear in a line.

Upvotes: 0

Santiago Rebella
Santiago Rebella

Reputation: 2449

Try to position both Divs with absolute inside a main div that could be relative. something like

#mainDiv {
    position:absolute; /* or relative depends how you have it*/
    width:80%;
    height:100%;
    left:10%;
}
#div1 {
    position:absolute;
    width: 40%;
    height:100%;
    left:0px;
    top:0px;
}

#div2 {
    position:absolute;
    width: 40%;
    height:100%;
    right:0px;
    top:0px;
}

Upvotes: 0

Sharon Simon
Sharon Simon

Reputation: 1

If you are using chrome using inspect element and find the corresponding 'div' tag and try to adjust the style(position)

Upvotes: 0

Related Questions