0

I have the following code which creates new spans with some input fields:

function add_course() {
     var newspan = document.createElement('span');
     newspan.innerHTML = 'Degree: <input type="text" name = "degree[]"> Course: <input type="text" name = "course[]">';
     document.getElementById('degree_course').appendChild(newspan); 
}

function add_skill() {
     var newspan = document.createElement('span');
     newspan.innerHTML = 'Skill: <input type="text" name = "skill[]"> Level: <input type="number"min ="1" max = "5" name = "level[]">';
     document.getElementById('skills').appendChild(newspan); 
}

I would like those spans to have a different id each. For example, in the case of add_course, if 3 spans where generated, i'd like them to have an id like: course_1, course_2, course_3 (skill_1, skill_2, skill_3 in the skills case).

I know I'll have to put and id="course_VAR" in the innerHTML and create a var that i'll inscrease everytime. (There won't be a for cicle, because the function will trigger when I hit a specific button).

<input class="buttons" type="button" id="more_fields" onclick="add_course();" value="Add Course" />

My question is, how can I get the var in the id and make it increase without a for cycle?

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
BVCGAAV
  • 301
  • 1
  • 4
  • 11
  • Just use a global variable (or one that's accessible from both functions) and increment it each time one of the functions is called. – FixMaker Apr 25 '15 at 20:40
  • already answered here: http://stackoverflow.com/questions/10126395/how-to-jquery-clone-and-change-id – Amit Apr 25 '15 at 20:43

3 Answers3

3

Put your var outside of function scope and increment it inside.

var idCounter = 0;

function add_course() {
  var newspan = document.createElement('span');
  newspan.id = 'course_' + idCounter++;
  newspan.innerHTML = 'Degree: <input type="text" name = "degree[]"> Course: <input type="text" name = "course[]">';
  document.getElementById('degree_course').appendChild(newspan)
}
Marcus Henrique
  • 762
  • 1
  • 5
  • 15
2

What you need to do it add a counter, then use setAttribute

var counter = 0;

function add_course() {
    var newspan = document.createElement('span');
    newspan.setAttribute('id', "course_"+counter);
    newspan.innerHTML = 'Degree: <input type="text" name = "degree[]"> Course: <input type="text" name = "course[]">';
    document.getElementById('degree_course').appendChild(newspan);
    counter += 1;
}
Downgoat
  • 13,771
  • 5
  • 46
  • 69
1

There's nothing inherently wrong with using a global or outer-scope variable to track this, as suggested in some of the other answers. But as an alternative, if you like to keep things 'tidy', you can do this:

var getIdFactory = function(pref) {
    return {
        counter: 0,
        prefix: pref,
        next: function() {
          this.counter++;
          return this.prefix+'_'+this.counter;
        }
    }
}

var courseId = getIdFactory('course');
var skillId = getIdFactory('skill');

Then, any time you need a new div ID, call (for instance) courseId.next().

function add_course() {
    var newspan = document.createElement('span');
    newspan.innerHTML = 'Degree: <input type="text" name = "degree[]"> Course: <input type="text" name = "course[]">';
    newspan.setAttribute('id', courseId.next());
    document.getElementById('degree_course').appendChild(newspan);
}

In your example here, this might be overkill. But one of the advantages here is that your id generation logic can be as complicated as you need, because it's self-contained.

JSfiddle here.

S McCrohan
  • 6,663
  • 1
  • 30
  • 39