Click here to Skip to main content
16,021,580 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
javascript function to create a table

function createTable(){
var contTable="<table id='test'><tbody>"
$.ajax({
url:'getData.php',
jsonp:'callback',
data:datastring,
success:function(data){
if(data!=null){
for(var h=0;h<data.length;h++){>
contTable+="<tr id="+data[h]+ID+"><td>"+data[h].NAME+"</td></tr>"
}
}
contTable+="</tbody></table></tr></tbody></table></thead></table>";
document.getElementById("conttable").innerHTML=contTable;
},
error:function(){
}
});
}
$(document).ready(function(){
$('#test tr').click(function(){
selectedid=$(this).attr('id');
});
});


html
HTML
<div id="conttable"></div>


if I create a table in html for example
XML
<table id="test">
<tr>
<td id="1">Name1</td>
</tr>
<tr>
<td id="2">Name2</td>
</tr>
<tr>
<td id="3">Name3</td>
</tr>
</table>

the click function works but not for a javascript table.What is wrong in this.Can anyone help me...
Posted
Updated 18-Jan-15 18:32pm
v7

First bug which I can see immediately: $('#test tr). Closing quotation mark is missing.

—SA
 
Share this answer
 
Comments
p@y@l 19-Jan-15 0:34am    
Thanks for pointing out that,but sorry that was my mistake while writing here.I updated the question.
Second bug: selectedid=$(this).attr('id'); does nothing. You just create a local object and assign something to it; eventually, this object will be discarded without any trace. How to "fix" it? It depends on what you are going to achieve by handling of the event. Do something reasonable.

Now tell me: are you going to iterate until people find all your bugs? Are you going to accept the fixes formally?

Look, I have long practice of reviewing works. It's customary to turn the work down after finding first blocking mistake. This is a very reasonable rule; otherwise, it would take to much of the reviewer's time spend in not a productive way. After all, you are the one who is interested in the final result the most. So, if you are going to wait for someone to produce a final working solution for you, better don't hold your breath.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900