Damian
Damian

Reputation: 31

dynamically created id in javascript

I need loop in Javascript - when You click on button it add ol tu ul and gave id with number, like this:

<ol id="single-task1"></ol>
<ol id="single-task2></ol>

And so on...

When I tried whit loop like this:

taskNumber = 0;

        for (var i; i<999; i++){
            taskNumber++;
        }

        var ol = document.createElement('ol'); //creating element ol
        taskValue = textField.value; //getting value from user
        taskList.appendChild(ol); //add ol to ul
        ol.id = 'single-task-'+taskNumber;
        ol.innerHTML = taskValue; //add value from user to new ol

All ol elements still have "single-task-0"... Probably loop is in wrong place or loop is wrong.

Upvotes: 0

Views: 42

Answers (1)

jmargolisvt
jmargolisvt

Reputation: 6088

You are confusing your taskNumber and your i variable. Initialize i = 0 in your loop and use that. Do all of your work within the loop. You don't need taskNumber.

Upvotes: 1

Related Questions