Home > Enterprise >  jquery .append() sometimes fail
jquery .append() sometimes fail

Time:10-22

I'm using jquery to add elements to a dynamic blank list.

I go through a loop like this in the script that's called from a dynamically created event handler. like this:

for (var o in dataArr) {
   var coreArr = dataArr[o],
       insStr = "";
   for (var op in coreArr) {
      insStr  = showInsData(op);
   }
   $("#"   o).append(insStr);
}

or

for (var o in dataArr) {
   var coreArr = dataArr[o];
   for (var op in coreArr) {
       var insStr = showInsData(op);
       $("#"   o).append(insStr);
   } 
}

two methods sometimes work and add the element.But sometimes two methods don't load the element. I had been tested that insStr and "#" o both hava value. But .append() is sometimes not working.

I really can't fix this issue. If anyone has a better solution, please let me know. Thank you very much !

CodePudding user response:

Don't write code like this.

var coreArr = dataArr[o],
       insStr = "";

Then you loop over coreArr[0], that's an array?

Tagging this as jquery is probably not the right problem. I think it might have to do with the data before it? Post more of the code and keep debugging.

CodePudding user response:

try this

$(`# ${o}`).append(insStr);

instead of

$("#" o).append(insStr);

you can use tilde sign (`) for concatenation

  • Related