0

I have a couple of divs that I created dynamically, which are added for documents that the user already seen.

doc_html.innerHTML = '<div id=' + doc_id + ' class="seen-doc"> <h5>You\'ve already seen this document.</h5></div>' + doc_html.innerHTML;

I also have this code to run when the user clicks the back button while on document, to return to the main menu. (I got this from https://stackoverflow.com/a/19196020/1122229)

<input type="hidden" id="refresh" value="no">
<script>
$(document).ready(function(e) {
        var $input = $('#refresh');
        $input.val() == 'yes' ? update_seen_docs() : $input.val('yes');
    });
</script>

What I need update_seen_docs() to do is look at the dynamic divs that I created earlier (with id equal todoc_id) and check if they exist or not. I already have the list of doc_ids that I want to check their existence. What I need is a way to check whether these dynamic div been created successfully by my earlier code.

I have tried putting this in my update_seen_docs()

doc_html = document.getElementById(doc_id);
if (doc_html){
    # do stuff
}

but since the divs with doc_id are dynamic, doc_html is always null.

Community
  • 1
  • 1
Algorithmatic
  • 1,824
  • 2
  • 24
  • 41
  • `id` should be unique..Are they unique in your case ? If there are many `id` attributes havins similar value, `getElementById` will return first matched element... – Rayon Mar 01 '16 at 04:03
  • the `id`s are unique. `getElementById` returns null because the divs are dynamic and not explicitly written in the html. – Algorithmatic Mar 01 '16 at 15:53
  • If you are trying to access elements after they are appended they it can not be null... – Rayon Mar 01 '16 at 17:31

0 Answers0