After a successful ajax response, I am attempting to apply a jQuery .css effect. However, it seems that this process is not working as expected because I am trying to retrieve the height of a newly created element and then applying it to another newly created element. My goal is to move the .opts div to the bottom based on the dynamic height of the .col-4 div.
You can view the code pen here: https://codepen.io/Pancuatico/pen/abpOeZy
Upon opening the code pen, you can execute this code in the console:
$(".opts").css("margin-top",$(".col-4").height());
and observe that it works perfectly. But how can I make this work following a successful ajax response?
Edit
Below is my JavaScript code where I might have made a mistake:
$(document).ready(function(){
loadImgs();
});
function loadImgs(){
$.post("anurl.php", function(data){
//Some data processing occurs here
//...
var out = "<div class='col-4'><img src='imgpathalt' alt='imgpath'></div>";
out += "<div class='col-2'>";
out += "<img src='imgpath2' alt='imgpath2alt'>";
out += "<div class='opts'>";
out += "<button>add</button>";
out += "<button>rm</button>";
out += "</div>";
out += "</div>";
$(".row").html(out);
var col4Height = $(".col-4").height();
$(".opts").css("margin-top",col4Height); //this does not work
});
}