Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing jQuery to gather dynamically added dom elements after page load
    primarykey
    data
    text
    <p>Alright so I recently received this project from a client. They needed a offline and online website that could be loaded via usb drive onto ipads or remote sources with no internet connection. So all of the files had to be ".html" extension. In order to make maintenance and changes easier I used javascript to load global portions of content into the pages, specifically the nav. I am having trouble trying to find the new elements I've added, here is a sample of the nav.tpl I'm loading via ajax into the dom:</p> <pre><code> &lt;li&gt;&lt;a href="index.html" class="nav-grey"&gt;&lt;span class="spacer"&gt;&lt;/span&gt;HOME&lt;span class="spacer"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt; &lt;li class="dropdown"&gt; &lt;a href="unmatched_simplicity.html" class="nav-red"&gt;unmatched &lt;br/&gt; SIMPLICITY&lt;/a&gt; &lt;ul class="dropdown-menu"&gt; &lt;li&gt;&lt;a href="unmatched_simplicity-easy_to_use.html"&gt;Easy to Use&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="unmatched_simplicity-cashier_workstation.html"&gt;Cashier Workstation Screen&lt;/a&gt;&lt;/li&gt; &lt;li class="dropdown-submenu"&gt; &lt;a tabindex="-1" href="unmatched_simplicity-fuel.html"&gt;Fuel&lt;/a&gt; &lt;ul class="dropdown-menu"&gt; &lt;li&gt;&lt;a tabindex="-1" href="unmatched_simplicity-fuel-authorize_pump.html"&gt;Authorize Pump&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a tabindex="-1" href="unmatched_simplicity-fuel-all_stop.html"&gt;All Stop&lt;/a&gt;&lt;/li&gt; </code></pre> <p>Here is the javascript:</p> <p>$.get( "lib/nav.tpl", function( navItems ) { $( ".nav" ).html( navItems ); });</p> <p>Now I have a naming convention I am using for organizational purposes as well as bread crumbs and now I am trying to use it to detect the nav item that the current page is on. Basically each level of pages is separated by "-". My goal was to use the url to find the anchor with href = to the current page, then go to its parent "li" and add the active class ".active". This is what I have:</p> <pre><code>( function( window ) { var location = locationTiers = ''; location = window.location.pathname; location = location.substr( location.indexOf('/') + 1 ); location = location.replace('.html', ''); locationTiers = location.split('-'); console.log( $( 'a[href="'+ locationTiers[0] +'.html"]' ).parent('li') ); }) ( window ); </code></pre> <p>It's strange because I can find the anchor if I remove ".parent('li')", but with the ".parent('li')" it returns empty. It seems like if it couldn't find the "li" it wouldn't be able to find the "a". I know about the .on() subsitute for live but this is mostly for detecting events. </p> <p>I was hoping I could do something like this:</p> <pre><code>$('body','a[href="'+ locationTiers[0] +'.html"]').parent('li') </code></pre> <p>Similar to .on live method but this doesn't work either. If anyone can help me figure out how I can find this dynamically added item it would be greatly appreciated!</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload