Results 1 to 5 of 5

Thread: [RESOLVED] Ajax dynamic content can't reference document classes or objects

  1. #1
    Join Date
    Aug 2011

    resolved [RESOLVED] Ajax dynamic content can't reference document classes or objects

    Hi All,

    I need a bit of help if possible? I've got a pretty good knowledge of websites, JS, etc.. and I've been playing with AJAX for dynamic content within divs on my pages to keep away from refreshing entire pages, etc..

    The problem.
    My website is for my photography. I have a static gallery (php) page within which I populate 1 DIV with dynamic html (an image tag, a few links, text, etc..) this works without fail.. Also... in this static page I have a DIV called AITrigger which is a simple CSS rollover image and tied to a jquery.click event to popup a div to present a purchase photo window. This works too..

    BUT I want the clickable AITrigger in the dynamic section since it varies in parameters, etc... so the AITrigger definition is in a CSS file, the query references to it are in the static gallery page and the actual AITrigger div is presented via AJAX.

    When I move the DIV AITrigger into the ajax callback and the AJAX presents the DIV dynamically then the jquery in the local document doesn't work. in fact I can use anything - doesn't have to be jquery, I've used bump box, lightbox, standard <A> tags, etc.. and none of them work once the call is made from the ajax code. move the code back onto the main document and it works..

    From my limited knowledge of the document structure I was thinking that the AJAX code doesn't exist at the time the main document is put together, therefore when the ajax code and links arrive they can't see the document or its content to call or reference them.

    Have I made sense here? its been bugging me for ages and I hoped someone could point me in a rough direction that I can figure out whats going on..



  2. #2
    Join Date
    Aug 2011
    Please explain it more precisely... When the content loaded via ajax is about to be displayed what do you do exactly?

    The basic question is when and where do you run a script which applies the behaviour of the AITrigger - do you run it every time the new content is loaded? If not then change it
    Can object-oriented JavaScript be more legible?

  3. #3
    Join Date
    Aug 2011

    more detail

    Hi thanks for replying,

    the ajax content is loaded by a standard callback and pushed into a DIV. It will contain a few html tags but also a DIV with an ID of aitrigger

    The static page has the class and jquery definitions for the div aitrigger. its job is to simply display a pop up div when clicked.

    When the ajax content is shown it displays normally and the trigger div is visible but when the div is clicked the jquery.onclick does not fire. nothing happens in fact.

    if the exact same code is placed on the static document it works. I've also tried this before with the ajax div calling a class such as bumpbox or mootools popups they also don't work if the trigger div is displayed via ajax. but put the div on the static document and it works!

    my thoughts were something along the lines of the ajax content being blind to the document structure?? and/or the jquery trying to attach itself to an element which doesn't exist before the ajax puts it on the page!?

    Hope this is clearer for you??


  4. #4
    Join Date
    Aug 2011
    Ok, so what you've just done will work only for static content. the jQuery script you are using applies the behaviour only to already existing elements... It means it just search for the matching element when after the page is loaded and that's it.

    Now, when you replace the part of content of a document and that content holds some element the script was applied to, you need to apply the script again. Because the elements are gone - doesn't matter if the IDs match... they are all fresh and their behaviour has to be set again.

    Your current code looks probably similar to:
      //your popup code
    So, what you need to do is:
    1. first wrap the script applying the "click" action in a function, e.g.:
      //define a function
      function applyClick(){
        //your popup code
    2. then use the function on document readyness
        //apply click behaviour when document is ready
    3. in your ajax callback always call applyClick() again, e.g.
      container.innerHTML = newContent;
    Last edited by desiter; 08-29-2011 at 12:00 PM.
    Can object-oriented JavaScript be more legible?

  5. #5
    Join Date
    Aug 2011

    Smile Thank you!

    Thats such a clear answer - It also means that I don't fully understand the document model and how the scripts attach but we all learn..

    Thank you so much for your answer - I can see whats happening now and I understand the fact that I have to reattach the jquery event to the 'new' id.

    I wish I'd asked you weeks ago about this..

    Thanks again - I really appreciate your answer to this..


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14289 seconds
  • Memory Usage 2,884KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates