Appending a div with id containing object

I'm trying to get every instance of an object to create html that displays the property values of that instance and no other. So I've added a property to the object prototype using jquery. The property adds html elements to the page. The elements I'm adding include ids which include the name of that particular object instance, which I use so I can display the property values of that particular instance only to that instance. But I run into a problem:

$('.manDivContainer').append("<div class='manDiv' id=this.nameLC ></div>");

If I use this code, the event thinks this.nameLC is a string, so every instance ends up having the same id.

$('.manDivContainer').append("<div class='manDiv' id="+ this.nameLC + "></div>");

If I use this code, on the other hand, adding the object in a space outside the quotes, the event doesn't work.

$('.manDivContainer').append("<div class='manDiv'></div>").attr('id', this.nameLC + "Div");

If I attempt to add an .attr, on the other hand, the code assumes I'm adding that attribute to every instance of .manDiv. So what will happen is with the first instance of this object I create, it adds the id of that object to .manDiv, but then when I create a second object after the first, the second object will add it's own id to both it's own .manDiv and the previous .manDiv, so I end up getting two divs with the same ID, which end up displaying the same information, instead of information unique to each object. I don't know how to work around this. Does anyone know if there's a solution?

Answers 1

  • you can isolate part of not working code to new variable

    var somethingToAdd = "here should be string with div";
    // here you can print it to console and see how it looks
    $('.manDivContainer').append(somethingToAdd);


Related Articles