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);
);
javascript jquery html json ajax
marked as duplicate by vlaz, charlietfl
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.
add a comment |
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);
);
javascript jquery html json ajax
marked as duplicate by vlaz, charlietfl
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
add a comment |
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);
);
javascript jquery html json ajax
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
javascript jquery html json ajax
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
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
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
add a comment |
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
add a comment |
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.
That's a comment, not an answer.
– Chris G
Nov 9 at 14:28
add a comment |
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.
That's a comment, not an answer.
– Chris G
Nov 9 at 14:28
add a comment |
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.
That's a comment, not an answer.
– Chris G
Nov 9 at 14:28
add a comment |
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.
You're creating the tr and td elements outside the loop. Re-Setting the innerHtml does not create new elements.
answered Nov 9 at 14:18
Ingo Bochmann
1023
1023
That's a comment, not an answer.
– Chris G
Nov 9 at 14:28
add a comment |
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
add a comment |
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