Mudsaf Posted February 26, 2015 Share Posted February 26, 2015 (edited) 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 February 26, 2015 by Mudsaf Link to comment Share on other sites More sharing options...
justsomeguy Posted February 26, 2015 Share Posted February 26, 2015 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. 1 Link to comment Share on other sites More sharing options...
Mudsaf Posted February 26, 2015 Author Share Posted February 26, 2015 Ah, thanks mate Link to comment Share on other sites More sharing options...
dsonesuk Posted February 27, 2015 Share Posted February 27, 2015 Try looking athttp://api.jquery.com/wrapall/Orhttp://api.jquery.com/wrap/ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now