I want to add a class called active to an element that is dynamically added with ajax jquery however the browser cannot find it which I'm assuming because it was dynamically added. I need a way to find the element without any events like clicking.
there is another post
jQuery - Find dynamically created element without events
but this one is different because I am trying find an element that was reloaded dynamically after the ajax success event. If its a bit confusing please check out my code below and the code on the similar but different post. Can someone give me some tips to find a dynamic element with out any click events or any other active event.(Passive events are fine like change or load) My html code
<div class="h-endless">
<div class="htmlgroup1container">
<a href="javascript:void(0)" data-group="group1" id="g-button1" class="g-button">firstButton</a>
<a href="javascript:void(0)" data-group="group2" id="g-button2" class="g-button">secondButton</a>
</div>
</div>
javascript/jquery code
$(document).ready(function(){
$("#addGroup").on('submit', function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'pf/formMail.php',
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function (data){
outputCheck(data.message);
if(data.message=="success"){
$.post("pf/formHTMLUPDATE.php", {
recall: "groups",
type: "group1"
}, function(data, status) {
$(".htmlgroup1container").html(data);
})
///$("#g-button1").addClass("active");///
}
},
error: function(xhr, status, error) {
$("#alert").css("background-color", "red");
$("#alert").css("display", "block");
$("#alert").html("Error<span class=\"closebtn\" onclick=\"this.parentElement.style.display='none';\">×</span>");
}
});
});
});
the ajax method basically replaces the html data in the element with the same thing initially inside(In the actual code it will replace it with links with different names more of them or less of them). The code I've posted is a bit watered down so that only the necessary code is shown.
I have highlighted or slashed //// the code that doesn't work. The active class just makes the button/link green. Thanks in advance