0

Context: In a form the user needs to add at least one responsible person before submitting the form. This is done with jQuery and Ajax. When submitting the form I need to check if at least one responsible was added using jQuery.

So I add a div with class "checkexists" in a div with ID "verantwoordelijke_added" as soon as the user click the plus button to add a repsonible.

PHP class returns this to jQuery:

$output_array["result"] = '<div id="responsadded_'.$respId.'" class="checkexists" style="line-height:25px">'.$firstname.' '.$lastname.' ('.$_GET["uid"].') <button type="button" class="close eua iid'.$respId.'" id="btn_rm_resp" title="Verwijder" aria-hidden="true">&times;</button></div>';
$output_array_json = json_encode($output_array);
echo $output_array_json;

jQuery appends this code to the div "verantwoordelijke_added":

$('#verantwoordelijke_added').append(data.result);

Then when submitting the form I would like to check if there are any divs with class "checkexists" in the containing div:

if ($("#verantwoordelijke_added").find(".checkexists").length > 0)

But even if there is a div appended, jQuery can't find it, I guess since it was added "on the fly".

Is there a way arround this?

Thanks a lot,

Mark

netcult
  • 167
  • 1
  • 1
  • 11
  • When are you performing the `length > 0` check? It would help to see all of your code. – Rory McCrossan Nov 22 '13 at 08:32
  • maybe your solution is [here](http://stackoverflow.com/questions/10415400/jquery-detecting-div-of-certain-class-has-been-added-to-dom), but I think you also should be able to detect the DOM element of the added div with jquery... – kave Nov 22 '13 at 08:35
  • dont rly understand this data.result. What is that? If you are trying to add a data tag to an html element, you should use $.data(); Maybe you shoudl show your whole code, only these fragments dont help us that much. – Realitätsverlust Nov 22 '13 at 08:39
  • well, the way i understand your question, if you need jquery to find it then you need to add the logic RIGHT AFTER you add the div, in your case add that if logic right after you append EVERY TIME – user2002495 Nov 22 '13 at 08:41
  • before this $('#verantwoordelijke_added').append(data.result); add console.log(data.result); . chekc whether data is rendered and recieved correctly from the backend – Bhadra Nov 22 '13 at 08:43
  • Ok, i missed out on something, apparently there is no problem checking if a div exists after it is created dynamically! Tnx Bhadra, checking the logs helped me find it! – netcult Nov 22 '13 at 09:05

0 Answers0