4

I have a div with an ID "orangeButton" and each time you click on it it creates a new div. This works fine but... I want each newly created div to have an incremental number added to it's ID.

I am not sure how to do this.

Here is a fiddle of the code I have thus far with comments.

http://jsfiddle.net/taoist/yPrab/1/

Thank you

Javascript Code

   var applicationArea = document.getElementById("applicationArea");
    var orangeButton = document.getElementById("orangeButton");


    orangeButton.onclick = function() {
      var newDivThingy = document.createElement("div");
      newDivThingy.id  = 'newDivThingy';  // I want each newly created div to have a      numeric value concatenated to it's ID. IE newDivThingy1 newDivThingy2 newDivThingy3
      applicationArea.appendChild(newDivThingy);


    };​
3
  • 2
    Just keep a variable var index = 0; and do newDivThingy.id = 'newDivThingy' + (index++);. Commented Jan 1, 2013 at 4:29
  • are you willing to use jquery? Commented Jan 1, 2013 at 4:30
  • 1
    No JQuery. I prefer to stick to raw Javascript for my own learning purposes. Felix seems like he has the answer I'm looking for. Will try it. Commented Jan 1, 2013 at 4:31

4 Answers 4

8

Am I missing something, why not use a counter?

var counter = 0;
button.onclick = function(){
   var newDivThingy = document.createElement("div");
   newDivThingy.id  = 'newDivThingy' + (++counter);
   // continue your stuff here     
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried playing with different ways to create a counter and couldn't figure out the proper syntax. I decided to ask instead of waste time.
@Taoist, Cool. Play with ++counter and counter++ to start from 0 or 1 for the <div> ID. I'd keep the initial counter at 0, that way it reflects current number of created items. Cheers!
1

Libraries like underscorejs provide a uniqueid function for this. Otherwise its easy to implement one.

myNamespace.uniqueId = (function () {
    var counter = 0; // in closure
    return function (prefix) {
        counter++;
        return (prefix || '') + '-' + counter; 
    };
}());

Usage.

newDiv.id = myNamespace.uniqueId('newDiv');

2 Comments

This looks cool. Just wondering... Is a return function(){....} making a copy of the function each time?
@AlexisWilke No, because the self executing function fires once. The result is uniqueId is assigned function (prefix) { ... having the counter variable in a closure (and its own private scope)
0

Simply use a integer and increment it as each element is added.

var applicationArea = document.getElementById("applicationArea"),
    orangeButton = document.getElementById("orangeButton"),
    counter = 1;

orangeButton.onclick = function() {
    var newDivThingy = document.createElement("div");
        newDivThingy.id = "newDivThingy" + counter++;
    applicationArea.appendChild(newDivThingy);
}

Comments

0

I have no doubt you have solution and may have forgotten this post. BUT, I wold like to show a solution that is a compact format.

Note the counter is set to (counter++) so it will start at 1.

var orangeButton = document.getElementById("orangeButton");
var counter = 0;
  orangeButton.onclick = function() {
    document.getElementById('applicationArea')
    .appendChild(document.createElement('div'))
    .setAttribute("id", 'newDivThingy' + counter++);
  // I want each newly created div to have a
  // numeric value concatenated to it's ID.
  // IE newDivThingy1 newDivThingy2 newDivThingy3
  };​

Comments

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.