0

I want to dynamically add table rows on change of my select I already wrote a script that actually does this, but the system is not dynamic yet.

<script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#noOfSegments").change(function(){ var counter=${#noOfSegments}.val(); for(i=0; i<counter; i++){ var row = $("<tr><td>"i"</td><td>"i"</td><td>"i"</td><td>"i"</td><td>"i"</td></tr>"); $("#segmentTable").append(row); } }); }); </script> <select id="noOfSegments"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> 

and #segmentTable is my table id but the above script gives me this error:

"Uncaught SyntaxError: Unexpected token { "

Can anyone please suggest me a solution?

4 Answers 4

3

You are missing a lot of parantheses, quotes and plus signs. This should work:

$(document).ready(function(){ $("#noOfSegments").change(function(){ var counter=$('#noOfSegments').val(); for(i=0; i<counter; i++){ var row = $("<tr><td>" + i + "</td><td>" + i + "</td><td>" + i + "</td><td>" + i + "</td><td>" + i + "</td></tr>"); $("#segmentTable").after(row.html()); } }); }); 
Sign up to request clarification or add additional context in comments.

1 Comment

.after(row.html()) does not append the row so its giving me a weird structure
1

DEMO

corrected code and

changed $("#segmentTable").after(row.html())

to

$("#segmentTable").html(row.html());

$(document).ready(function () { $("#noOfSegments").change(function () { var counter = $('#noOfSegments').val(); for (i = 0; i < counter; i++) { var row = $("<tr><td>" + i + "</td><td>" + i + "</td><td>" + i + "</td><td>" + i + "</td><td>" + i + "</td></tr>"); $("#segmentTable").html(row.html()); } }); }); 

Comments

0

Change ${#noOfSegments} to $('#noOfSegments').

Comments

0

In java script you can do pure dynamically

var table = document.createElement("table"); table.id = "tbldrp"; var row = table.insertRow(table.rows.length); var rowItems = row.insertCell(0); var label = document.createElement("label"); label.id = "lblDripName"; label.innerHTML = "Label Content"; rowItems.appendChild(label); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.