Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery get(), Shadowbox, return the whole html of the containing div
    primarykey
    data
    text
    <p>Related to my earlier question (thanks for your response Cryo), I have a further question for the javaScript mavens here.</p> <p>Given an html structure like this:</p> <pre><code>&lt;content&gt; &lt;div entry&gt; &lt;div rubric&gt; &lt;a name&gt; &lt;/div&gt; &lt;div content block&gt; &lt;p&gt; &lt;a links&gt; &lt;/div&gt; &lt;/div entry&gt; . . . multiple identical entry divs &lt;/content&gt; </code></pre> <p>Using jQuery I wanted to find a given anchor name, and return the whole html markup of its containing entry div, with a new classname, as a string that can be passed to shadowbox's html player like this:</p> <pre><code>""" &lt;div newclassname&gt; &lt;div rubric&gt; &lt;a name&gt; &lt;/div&gt; &lt;div content block&gt; &lt;p&gt; &lt;a links&gt; &lt;/div&gt; &lt;/div&gt; """ </code></pre> <p>The following code does this, but with one wrinkle, it reverses the document order of &lt;div rubric&gt; and &lt;div content block&gt;.</p> <pre><code>var $pageEntry = null; jQuery.get('thatPage.html', function(data){ $pageEntry = jQuery(data).find("#content"); }); function displayEntry(entry){ html = $pageEntry.find("a[name='" + entry + "']") .parent() .siblings() .andSelf() .wrapAll('&lt;div class="newclassname"&gt;&lt;/div') .parent() .parent() .html() Shadowbox.open({ options: { enableKeys: false }, player: 'html', content: html }); }; </code></pre> <p>Can anyone suggest a less verbose jQuery chain that retains the document order of the elements in the returned string? Any helpful suggestions or observations would be welcome. If there's a more straightforward way to do this in straight js, I'd love to know that too.</p> <p>Thanks,</p> <p>jjon</p> <p>update: the answer thanks to comments from Pointy:</p> <pre><code>elem = $("#content") .find("a[name=entry]") .parent() .parent() .wrapAll('&lt;div class="new"&gt;&lt;/div') .parent() .html() </code></pre>
    singulars
    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.
    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