A.E A.E - 16 days ago 4
Javascript Question

Id variable does not work when I want to use it as reference

var lightcount = 0;
$("#addlight").click(function(){
var domElement = $('<div id="L' + lightcount +'" class="col-md-3 col-sm-6 text-center"><div class="rumbox"></div><button id="onoff" type="button" class="btn btn-md btn-success"> ON </button></div>');
var lid= $(domElement).attr("id");
console.log(lid);

$(lid).after(domElement);
lightcount++
});


I have a function that creates a new lightbox when clicking a button.
I want the new lightboxes to be places after current one.

I have a variable for the div id that is incremented for every new lightbox.

So instead of
$("#L3").after(domElement)
(which actually works),
I want to use my variable lid:
$(lid).after(domElement);
But this does not work.

Answer

You could try:

$("#" + lid).after(domElement);