-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestions.js
More file actions
115 lines (95 loc) · 2.73 KB
/
Copy pathquestions.js
File metadata and controls
115 lines (95 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var formulario = document.getElementById('formQuestion');
var btn = document.getElementById('addQuestion');
var btnSubmit = document.getElementById('btnSubmit');
var cadenaPreguntas = [" ¿En cuánto tiempo estimas alcanzar el punto de equilibrio?", " Para comercializar, ¿cuál será el canal de ventas principal?", "¿Existen sustitutos (competidores indirectos) a tu producto o servicio?", " ¿En cuánto tiempo estarías vendiendo con una inversión de un millón de dólares?"];
var preguntas = [];
generateQuestions();
btn.addEventListener("click", function (event) {
var descr = document.getElementById('descr').value;
if (descr !== "")
{
pregunta = new Pregunta(descr);
generateNode(pregunta);
console.log(preguntas);
}
else
{
alert("Debe escribir una descripción de la pregunta");
}
event.preventDefault();
});
btnSubmit.addEventListener("click", function (event)
{
for(var i = 0; i < preguntas.length; i++)
{
preguntas[i].respuesta = document.getElementById(preguntas[i].id).value;
}
console.log(preguntas);
alert("Guardado");
event.preventDefault();
});
function generateQuestions()
{
for(var i = 0; i < cadenaPreguntas.length; i++) {
pregunta = new Pregunta(cadenaPreguntas[i]);
generateNode(pregunta);
}
}
function generateNode(pregunta)
{
var label = document.createElement("label");
var input = document.createElement("textarea");
input.className = 'form-control';
var link = document.createElement("a");
link.href = "#!";
link.innerHTML = "x";
link.className = "delete";
link.className += " btn";
link.className += " btn-danger";
link.addEventListener("click", deleteQuestion);
var aux = parseInt(preguntas.length) + 1;
input.id = "p" + aux;
input.name = "p" + aux;
label.id = "label" + input.name;
label.innerHTML = pregunta.pregunta + ":";
link.dataset.id = input.id;
link.id = "link" + input.id;
pregunta.id = input.id;
formulario.appendChild(link);
formulario.appendChild(label);
formulario.appendChild(input);
preguntas.push(pregunta);
console.log(preguntas);
}
function deleteQuestion()
{
var input = document.getElementById(this.dataset.id);
var label = document.getElementById("label" + this.dataset.id);
var link = document.getElementById("link" + input.id);
formulario.removeChild(input);
formulario.removeChild(label);
formulario.removeChild(link);
console.log(preguntas);
var key = findById(input.id);
console.log('key: ' + key);
preguntas.splice(key, key + 1);
console.log(preguntas);
this.preventDefault;
}
function Pregunta(pregunta)
{
this.id,
this.pregunta = pregunta,
this.respuesta
}
function findById(id)
{
for(var i = 0; i < preguntas.length; i++)
{
if (preguntas[i].id === id)
{
return i;
}
}
throw "Couldn't find object with id: " + id;
};