1

I am trying to load DIV on button click inside another DIV and on another click it should create a new DIV inside the newly created DIV. Here is my fiddle:http://jsfiddle.net/LzCW5/4/ and my code:

HTML:

<a href="#" onclick="nestDiv()">Generate</a>
<div id='x0'>
    0
</div>

JavaScript:

int level=1;
function nestDiv()
{
    document.getElementById('x'+(level-1)).innerHTML="<div id='x"+level+"'>"+level+"</div>";
    level++;
    if(level==5)
        //do somthing
}

I also want to perform some special operation when nesting level reaches 5. I am not so pro at JavaScript. So please tell me how can I achieve this?

7
  • 1
    jsfiddle.net/adeneo/LzCW5/7 Commented May 17, 2014 at 16:39
  • awesome @adeneo. Please add the code as your answer. Commented May 17, 2014 at 16:43
  • Nah, someone will steal it any minute now and post it, or post something better. If not, you can post it and accept the answer yourself. Commented May 17, 2014 at 16:44
  • Nice one @adeneo I followed different approach though Commented May 17, 2014 at 16:51
  • 1
    There is no int in javascript, only var and const, so using int is a syntax error Commented May 18, 2014 at 8:08

3 Answers 3

1

In your code you only have to change int to var and call the function as a variable:

var level=1;
nestDiv = function()
{
    document.getElementById('x'+(level-1)).innerHTML="<div id='x"+level+"'>"+level+"</div>";
    level++;
}

You can see this working here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pure JS solution. But can you tell me how using var instead of int effected the output so much?
Of course, you can see parseInt function here. w3schools.com/jsref/jsref_parseint.asp
1

Here's how to append a div in jQuery:

var newDiv = $("<div id='x"+level+"'>"+level+"</div>");
$('#x'+(level-1)).append(newDiv);

This should replace your document.getElementById... line

1 Comment

Thanks @Nicolas78. +1 to you but I marked answer by NEOLPAR, as the solutions given by adeneo and NEOLPAR are in pure JS. :)
1

My understanding as per your question:

When you click a button, add a div to another div.

Now when the user clicks another button, new div must be added in the previously created new div.

enter image description here

so let's break it down:

$("button").click(function() {
    if($("#referenceDivId").children().length == 0) {
        // if there are no children initially
        $("#referenceDivId").append("<div>New div</div>");
    } else if($("#referenceDivId").children().length == 5) {
        // do your magic when children divs are 5
    }
    else {
        // if it already had children
        $("#referenceDivId").find("div:last").append("<div>New div</div>");   
    }
});

DEMO

1 Comment

Thanks @mohamedrias. +1 to you but I marked the answer by NEOLPAR, as the solutions given by adeneo and NEOLPAR are in pure JS. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.