Why is it setting I to the response lenght? [duplicate]









up vote
0
down vote

favorite













This question already has an answer here:



  • JavaScript closure inside loops – simple practical example

    39 answers



I'm trying to create a table from an ajax json response but my for loop sets i to the max value instantly.



$.ajax(settings).done(function (response) 
console.log(response);
var i = "";
td = document.createElement('td');
tr = document.createElement('tr');
tableContent = document.getElementById('analysisTable');
for (i = 0; i < response.segments.length; ++i)
td.innerHTML = response.segments[i].confidence;
$(td).attr(
'id': i,
'class': "analysis",
);
tableContent.appendChild(tr).appendChild(td);

);









share|improve this question















marked as duplicate by vlaz, charlietfl html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 9 at 14:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You're probably overwriting your previous appends every time.
    – Shilly
    Nov 9 at 14:15






  • 2




    You are creating a single <tr> and a single <td>. You will at the very least need to create a <td> for each segment element.
    – Chris G
    Nov 9 at 14:17










  • Here's the proper jQuery way to do this: jsfiddle.net/khrismuc/ycn9qspw
    – Chris G
    Nov 9 at 14:23










  • "my for loop sets i to the max value instantly." if you use the debugger to step through your code it will be trivial to see that this is not what is happening at all. The symptoms of your problem might make it look like that, but as others have said, it's because you keep overwriting the same variable every time, so all references to that variable which you add to the DOM will contain the same content (and since it's a loop, it'll be whatever the content was the last time the loop ran).
    – ADyson
    Nov 9 at 14:29














up vote
0
down vote

favorite













This question already has an answer here:



  • JavaScript closure inside loops – simple practical example

    39 answers



I'm trying to create a table from an ajax json response but my for loop sets i to the max value instantly.



$.ajax(settings).done(function (response) 
console.log(response);
var i = "";
td = document.createElement('td');
tr = document.createElement('tr');
tableContent = document.getElementById('analysisTable');
for (i = 0; i < response.segments.length; ++i)
td.innerHTML = response.segments[i].confidence;
$(td).attr(
'id': i,
'class': "analysis",
);
tableContent.appendChild(tr).appendChild(td);

);









share|improve this question















marked as duplicate by vlaz, charlietfl html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 9 at 14:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You're probably overwriting your previous appends every time.
    – Shilly
    Nov 9 at 14:15






  • 2




    You are creating a single <tr> and a single <td>. You will at the very least need to create a <td> for each segment element.
    – Chris G
    Nov 9 at 14:17










  • Here's the proper jQuery way to do this: jsfiddle.net/khrismuc/ycn9qspw
    – Chris G
    Nov 9 at 14:23










  • "my for loop sets i to the max value instantly." if you use the debugger to step through your code it will be trivial to see that this is not what is happening at all. The symptoms of your problem might make it look like that, but as others have said, it's because you keep overwriting the same variable every time, so all references to that variable which you add to the DOM will contain the same content (and since it's a loop, it'll be whatever the content was the last time the loop ran).
    – ADyson
    Nov 9 at 14:29












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • JavaScript closure inside loops – simple practical example

    39 answers



I'm trying to create a table from an ajax json response but my for loop sets i to the max value instantly.



$.ajax(settings).done(function (response) 
console.log(response);
var i = "";
td = document.createElement('td');
tr = document.createElement('tr');
tableContent = document.getElementById('analysisTable');
for (i = 0; i < response.segments.length; ++i)
td.innerHTML = response.segments[i].confidence;
$(td).attr(
'id': i,
'class': "analysis",
);
tableContent.appendChild(tr).appendChild(td);

);









share|improve this question
















This question already has an answer here:



  • JavaScript closure inside loops – simple practical example

    39 answers



I'm trying to create a table from an ajax json response but my for loop sets i to the max value instantly.



$.ajax(settings).done(function (response) 
console.log(response);
var i = "";
td = document.createElement('td');
tr = document.createElement('tr');
tableContent = document.getElementById('analysisTable');
for (i = 0; i < response.segments.length; ++i)
td.innerHTML = response.segments[i].confidence;
$(td).attr(
'id': i,
'class': "analysis",
);
tableContent.appendChild(tr).appendChild(td);

);




This question already has an answer here:



  • JavaScript closure inside loops – simple practical example

    39 answers







javascript jquery html json ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 14:13









George

4,39711731




4,39711731










asked Nov 9 at 14:12









isetnt

12




12




marked as duplicate by vlaz, charlietfl html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 9 at 14:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by vlaz, charlietfl html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 9 at 14:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • You're probably overwriting your previous appends every time.
    – Shilly
    Nov 9 at 14:15






  • 2




    You are creating a single <tr> and a single <td>. You will at the very least need to create a <td> for each segment element.
    – Chris G
    Nov 9 at 14:17










  • Here's the proper jQuery way to do this: jsfiddle.net/khrismuc/ycn9qspw
    – Chris G
    Nov 9 at 14:23










  • "my for loop sets i to the max value instantly." if you use the debugger to step through your code it will be trivial to see that this is not what is happening at all. The symptoms of your problem might make it look like that, but as others have said, it's because you keep overwriting the same variable every time, so all references to that variable which you add to the DOM will contain the same content (and since it's a loop, it'll be whatever the content was the last time the loop ran).
    – ADyson
    Nov 9 at 14:29
















  • You're probably overwriting your previous appends every time.
    – Shilly
    Nov 9 at 14:15






  • 2




    You are creating a single <tr> and a single <td>. You will at the very least need to create a <td> for each segment element.
    – Chris G
    Nov 9 at 14:17










  • Here's the proper jQuery way to do this: jsfiddle.net/khrismuc/ycn9qspw
    – Chris G
    Nov 9 at 14:23










  • "my for loop sets i to the max value instantly." if you use the debugger to step through your code it will be trivial to see that this is not what is happening at all. The symptoms of your problem might make it look like that, but as others have said, it's because you keep overwriting the same variable every time, so all references to that variable which you add to the DOM will contain the same content (and since it's a loop, it'll be whatever the content was the last time the loop ran).
    – ADyson
    Nov 9 at 14:29















You're probably overwriting your previous appends every time.
– Shilly
Nov 9 at 14:15




You're probably overwriting your previous appends every time.
– Shilly
Nov 9 at 14:15




2




2




You are creating a single <tr> and a single <td>. You will at the very least need to create a <td> for each segment element.
– Chris G
Nov 9 at 14:17




You are creating a single <tr> and a single <td>. You will at the very least need to create a <td> for each segment element.
– Chris G
Nov 9 at 14:17












Here's the proper jQuery way to do this: jsfiddle.net/khrismuc/ycn9qspw
– Chris G
Nov 9 at 14:23




Here's the proper jQuery way to do this: jsfiddle.net/khrismuc/ycn9qspw
– Chris G
Nov 9 at 14:23












"my for loop sets i to the max value instantly." if you use the debugger to step through your code it will be trivial to see that this is not what is happening at all. The symptoms of your problem might make it look like that, but as others have said, it's because you keep overwriting the same variable every time, so all references to that variable which you add to the DOM will contain the same content (and since it's a loop, it'll be whatever the content was the last time the loop ran).
– ADyson
Nov 9 at 14:29




"my for loop sets i to the max value instantly." if you use the debugger to step through your code it will be trivial to see that this is not what is happening at all. The symptoms of your problem might make it look like that, but as others have said, it's because you keep overwriting the same variable every time, so all references to that variable which you add to the DOM will contain the same content (and since it's a loop, it'll be whatever the content was the last time the loop ran).
– ADyson
Nov 9 at 14:29












1 Answer
1






active

oldest

votes

















up vote
-2
down vote













You're creating the tr and td elements outside the loop. Re-Setting the innerHtml does not create new elements.






share|improve this answer




















  • That's a comment, not an answer.
    – Chris G
    Nov 9 at 14:28

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
-2
down vote













You're creating the tr and td elements outside the loop. Re-Setting the innerHtml does not create new elements.






share|improve this answer




















  • That's a comment, not an answer.
    – Chris G
    Nov 9 at 14:28














up vote
-2
down vote













You're creating the tr and td elements outside the loop. Re-Setting the innerHtml does not create new elements.






share|improve this answer




















  • That's a comment, not an answer.
    – Chris G
    Nov 9 at 14:28












up vote
-2
down vote










up vote
-2
down vote









You're creating the tr and td elements outside the loop. Re-Setting the innerHtml does not create new elements.






share|improve this answer












You're creating the tr and td elements outside the loop. Re-Setting the innerHtml does not create new elements.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 14:18









Ingo Bochmann

1023




1023











  • That's a comment, not an answer.
    – Chris G
    Nov 9 at 14:28
















  • That's a comment, not an answer.
    – Chris G
    Nov 9 at 14:28















That's a comment, not an answer.
– Chris G
Nov 9 at 14:28




That's a comment, not an answer.
– Chris G
Nov 9 at 14:28



Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)