Jump to content

jQuery append wont go to end of line


Mudsaf

Recommended Posts

Hello, i'm wondering why my JavaScript / jQuery code doesn't work.

$(function() {var i=0;$("#preimgarea").prepend("<center>");bg_imglist.forEach(function(entry) {$("#preimgarea").append("<img style='someCSS' class='setBG' id='" + i + "' src='" + entry[0] + "'>");i++;if (i == bg_imglist.length) {$("#preimgarea").append("</center>");}});	});

Basically the code pastes <center></center> then the forEach data.

Edited by Mudsaf
Link to comment
Share on other sites

It sounds like using prepend or append is the same as using innerHTML. When you prepend that first <center> tag, the browser will create a center element and automatically close it, since a non-closed center element is not an element. You should probably build your HTML in a string and then append the entire thing after the loop. You shouldn't use a center element though, that's been deprecated for years. It was deprecated in HTML 4, which came out in 1997, and has since been removed completely.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...