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?