I am developing an application where the user has to input a name and then there is a progress bar that must be set the value in the input text type (or numeric type). I want the progress bar to automatically have that value.
I used this HTML code:
JavaScript
x
17
17
1
<table id="table">
2
<thead>
3
<tr>
4
<th>Name</th>
5
<th>Score</th>
6
</tr>
7
</thead>
8
</table>
9
10
<label class="form-control-label" for="name">Name:</label>
11
<input class="form-control" name="name" id="name" type="text">
12
<button class="btn btn-primary" onclick="addHtmlTableRow();">Add Data</button>
13
14
<label class="form-control-label" for="score">Score:</label>
15
<input class="form-control" name="score" id="score" type="number">
16
<button class="btn btn-primary" onclick="addHtmlTableRowScore();">Add Score</button>
17
and my javascript code is :
JavaScript
1
16
16
1
function addHtmlTableRow(){
2
var table = document.getElementById("table"),
3
newRow = table.insertRow(table.length),
4
cell1 = newRow.insertCell(0),
5
cell2 = newRow.insertCell(1),
6
name = document.getElementById("name").value;
7
8
cell1.innerHTML = name;
9
cell2.innerHTML = "<div class="progress"><div class="progress-bar" id="score-progress" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div></div>";
10
}
11
12
function addHtmlTableRowScore(){
13
var student_score = document.getElementById("score-progress");
14
student_score.value = document.getElementById("score").value;
15
}
16
Advertisement
Answer
it better to add the data using one function so it easy to validate and doesn’t require element ID
.
You can’t use document.getElementById("score").value
because its not <progress>
tag, use attribute style="width: 50%"
.
JavaScript
1
21
21
1
function addHtmlTableRow() {
2
var score = document.getElementById('score').value,
3
name = document.getElementById('name').value;
4
5
//validate score
6
if (!name || !score) {
7
console.log('name and score are required!');
8
return;
9
}
10
11
var table = document.getElementById('table'),
12
newRow = table.insertRow(table.length),
13
cell1 = newRow.insertCell(0),
14
cell2 = newRow.insertCell(1);
15
16
cell1.innerHTML = name;
17
cell2.innerHTML =
18
'<div class="progress"><div class="progress-bar" style="width: ' +
19
score +
20
'%" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div></div>';
21
}
JavaScript
1
14
14
1
body{padding: 10px;}
2
table, td, th {
3
border: 1px solid #bbb;
4
}
5
6
table {
7
width: 100%;
8
border-collapse: collapse;
9
}
10
11
td:nth-child(2){
12
width: 80%;
13
padding: 5px;
14
}
JavaScript
1
19
19
1
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
2
3
<table id="table">
4
<thead>
5
<tr>
6
<th>Name</th>
7
<th>Score</th>
8
</tr>
9
</thead>
10
</table>
11
12
<label class="form-control-label" for="name">Name:</label>
13
<input class="form-control" name="name" id="name" type="text" />
14
<label class="form-control-label" for="score">Score:</label>
15
<input class="form-control" name="score" id="score" type="number" />
16
<br />
17
<button class="btn btn-primary" onclick="addHtmlTableRow();">
18
Add Row Data
19
</button>