Good morning all. I have an issue that's stopping my project progression until I can get through it. So, I thought I'd ask you fine folks for an assist!
The situation is this: I have to provide a dynamically growing table to the users that will allow them to enter 1-N records for returns. So they load the page, click the button, and a new row appears. I am glad to say that my code works, with one major flaw - the table rows repeat!! So if the user were to click the "Add" button twice, I'd expect to have them see:
+---+------+------+----------+
| # | Year | week | Quantity |
+---+------+------+----------+
| 1 | |v|| |v|| ________ |
+---+------+------+----------+
| 2 | |v|| |v|| ________ |
+---+------+------+----------+
Instead they see this:
+---+------+------+----------+
| # | Year | week | Quantity |
+---+------+------+----------+
| 1 | |v|| |v|| ________ |
+---+------+------+----------+
| 2 | |v|| |v|| ________ |
+---+------+------+----------+
| 1 | |v|| |v|| ________ |
+---+------+------+----------+
| 2 | |v|| |v|| ________ |
+---+------+------+----------+
It's the weirdest thing to me. This code's not in a loop or anything - so it should just get one row!! Relevant code in my Fiddle here, as well as below (snippets for question purposes)...
HTML
<fieldset class="fieldset2">
<legend>Return Specific Curriculum Information</legend>
<input type="hidden" id="ccnt" value="0">
<table class="table" id="retc">
<tr>
<th class="th">Entry #</th>
<th class="th">Year</th>
<th class="th">Week/Packet</th>
<th class="th">Quantity</th>
</tr>
<tbody>
</tbody>
</table>
<input type="button" value="Add Curriculum To Return" class="button" id="addcurric">
<input type="button" value="Remove All Entries" class="button" id="remcurric">
</fieldset>
JQuery/JavaScript
$('#ccnt').data('count', 0);
$('#addcurric').click(function () {
function getcount() {
var $this = $('#ccnt'),
count = $this.data('count') + 1;
$this.data('count', count);
return count;
}
var mycount = getcount();
//console.log('mycount: ' + mycount);
var tdclass;
if (mycount % 2 == 1) {
tdclass = "td1";
} else {
tdclass = "td2";
}
//console.log('tdclass: ' + tdclass);
//window.alert(mycount + ' ' + tdclass);
var chtml = "";
chtml += "'<tr>";
chtml += " <td class=\"" + tdclass + "\">" + mycount + "</td>\\n";
chtml += " <td class=\"" + tdclass + "\"><select name=\"HYear" + mycount + "\" id=\"hyear" + mycount + "\">\\n";
chtml += " <option value=\"0\">-- Select --</option>\\n";
chtml += " <option value=\"1\">Year 1</option>\\n";
chtml += " <option value=\"2\">Year 2</option>\\n";
chtml += " <option value=\"3\">Year 3</option>\\n";
chtml += " </select></td>\\n";
chtml += " <td class=\"" + tdclass + "\"><select name=\"Week" + mycount + "\" id=\"week" + mycount + "\">\\n";
chtml += " <option value=\"0\">-- Select --</option>\\n";
chtml += " <option value=\"1\">Week 1</option>\\n";
chtml += " <option value=\"2\">Week 2</option>\\n";
chtml += " <option value=\"3\">Week 3</option>\\n";
chtml += " </select></td>\\n";
chtml += " <td class=\"" + tdclass + "\"><input type=\"text\" name=\"qty" + mycount + "\" class=\"input\"></td>\\n";
chtml += " </tr>\\n'";
//console.log('chtml is: ' + chtml);
//window.alert(chtml);
//console.log("Writing an iteration of chtml to scrren...");
$('#retc > tbody').append(chtml);
});
Can someone out there help me understand how/why I'm getting a duplicate row entry for each time the button is clicked?
Thanks in advance!!
Good morning all. I have an issue that's stopping my project progression until I can get through it. So, I thought I'd ask you fine folks for an assist!
The situation is this: I have to provide a dynamically growing table to the users that will allow them to enter 1-N records for returns. So they load the page, click the button, and a new row appears. I am glad to say that my code works, with one major flaw - the table rows repeat!! So if the user were to click the "Add" button twice, I'd expect to have them see:
+---+------+------+----------+
| # | Year | week | Quantity |
+---+------+------+----------+
| 1 | |v|| |v|| ________ |
+---+------+------+----------+
| 2 | |v|| |v|| ________ |
+---+------+------+----------+
Instead they see this:
+---+------+------+----------+
| # | Year | week | Quantity |
+---+------+------+----------+
| 1 | |v|| |v|| ________ |
+---+------+------+----------+
| 2 | |v|| |v|| ________ |
+---+------+------+----------+
| 1 | |v|| |v|| ________ |
+---+------+------+----------+
| 2 | |v|| |v|| ________ |
+---+------+------+----------+
It's the weirdest thing to me. This code's not in a loop or anything - so it should just get one row!! Relevant code in my Fiddle here, as well as below (snippets for question purposes)...
HTML
<fieldset class="fieldset2">
<legend>Return Specific Curriculum Information</legend>
<input type="hidden" id="ccnt" value="0">
<table class="table" id="retc">
<tr>
<th class="th">Entry #</th>
<th class="th">Year</th>
<th class="th">Week/Packet</th>
<th class="th">Quantity</th>
</tr>
<tbody>
</tbody>
</table>
<input type="button" value="Add Curriculum To Return" class="button" id="addcurric">
<input type="button" value="Remove All Entries" class="button" id="remcurric">
</fieldset>
JQuery/JavaScript
$('#ccnt').data('count', 0);
$('#addcurric').click(function () {
function getcount() {
var $this = $('#ccnt'),
count = $this.data('count') + 1;
$this.data('count', count);
return count;
}
var mycount = getcount();
//console.log('mycount: ' + mycount);
var tdclass;
if (mycount % 2 == 1) {
tdclass = "td1";
} else {
tdclass = "td2";
}
//console.log('tdclass: ' + tdclass);
//window.alert(mycount + ' ' + tdclass);
var chtml = "";
chtml += "'<tr>";
chtml += " <td class=\"" + tdclass + "\">" + mycount + "</td>\\n";
chtml += " <td class=\"" + tdclass + "\"><select name=\"HYear" + mycount + "\" id=\"hyear" + mycount + "\">\\n";
chtml += " <option value=\"0\">-- Select --</option>\\n";
chtml += " <option value=\"1\">Year 1</option>\\n";
chtml += " <option value=\"2\">Year 2</option>\\n";
chtml += " <option value=\"3\">Year 3</option>\\n";
chtml += " </select></td>\\n";
chtml += " <td class=\"" + tdclass + "\"><select name=\"Week" + mycount + "\" id=\"week" + mycount + "\">\\n";
chtml += " <option value=\"0\">-- Select --</option>\\n";
chtml += " <option value=\"1\">Week 1</option>\\n";
chtml += " <option value=\"2\">Week 2</option>\\n";
chtml += " <option value=\"3\">Week 3</option>\\n";
chtml += " </select></td>\\n";
chtml += " <td class=\"" + tdclass + "\"><input type=\"text\" name=\"qty" + mycount + "\" class=\"input\"></td>\\n";
chtml += " </tr>\\n'";
//console.log('chtml is: ' + chtml);
//window.alert(chtml);
//console.log("Writing an iteration of chtml to scrren...");
$('#retc > tbody').append(chtml);
});
Can someone out there help me understand how/why I'm getting a duplicate row entry for each time the button is clicked?
Thanks in advance!!
Share Improve this question asked Feb 26, 2015 at 13:12 TheJester1977TheJester1977 1551 silver badge10 bronze badges 3- Change $('#retc > tbody').append(chtml) to $('#retc > tbody').html(chtml) – lshettyl Commented Feb 26, 2015 at 13:15
- possible duplicate of Adding rows to tbody of a table using jquery – user557419 Commented Feb 26, 2015 at 13:18
- @Daniel-Ziga - Nope, not a duplicate... :) – TheJester1977 Commented Feb 26, 2015 at 13:43
2 Answers
Reset to default 6Your html fragment is lacking a thead
element. The following structure of the table element makes it work.
<table class="table" id="retc">
<thead>
<tr>
<th class="th">Entry #</th>
<th class="th">HIPPY Year</th>
<th class="th">Week/Packet</th>
<th class="th">Quantity</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Live Demo here.
Explanation
iirc modern browsers make sure that tr
elements are wrapped with a tbody
in the dom. This way you end up with 2 elements matching the selector of the element(s) on which to apply the appending.
You need to either empty the cotainer first or use .html
$('#retc > tbody').empty().append(chtml);
OR
$('#retc > tbody').html(chtml);