Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting a group of nodes and wrap in a <div>
    primarykey
    data
    text
    <p>I'm trying to write a greasemonkey script for a rather unpleasantly structured page. I'd like to be able to show and hide different entries, but they're in the form:</p> <pre><code>&lt;a name="first"/&gt; &lt;h3&gt;First&lt;/h3&gt; Some text.... &lt;!-- end --&gt; &lt;a name="second"/&gt; &lt;h3&gt;Second&lt;/h3&gt; Some text.... &lt;!-- end --&gt; </code></pre> <p>I was hoping to be able to wrap these in &lt;div&gt;s, e.g.:</p> <pre><code>&lt;a name="first"/&gt; &lt;div name="first&gt; &lt;h3&gt;First&lt;/h3&gt; Some text.... &lt;!-- end --&gt; &lt;/div&gt; </code></pre> <p>How can I do this? I was thinking of using the prev ~ siblings jQuery selector, then iterating through until I reach another matching , but then (a) I have to match the next manually, and (b) I'm not sure how then to put them inside the when that's done?</p> <p>Thanks!</p> <p>PS. Why did I have to use &amp;lt; and &amp;gt; to write &lt;div&gt; instead of angle braces?</p> <p><strong>UPDATE:</strong></p> <p>So far, I have:</p> <pre><code>$(document).ready(function(){ var course = $("meta[name=DEPARTMENT]").attr("content").replace(/^\s+/, ""); $("a[name^="+course+".]:first").each(function(){ GM_log(this.name); var stop=false; $(this).nextAll().filter(function(){ if ($(this).is("a[name^="+course+".]")) { stop=true; } return !stop; }) .wrapAll(document.createElement("div")); }); }); </code></pre> <p>However, this leaves the orphaned text behind:</p> <pre><code>&lt;a name="first"/&gt; &lt;div name="first&gt; &lt;h3&gt;First&lt;/h3&gt; &lt;!-- end --&gt; &lt;/div&gt; Some text... </code></pre> <p><strong>UPDATE 2:</strong> It turns out that the nextAll function is written to only return element nodes. I wrote a replacement:</p> <pre><code>$.fn.nextAllWithText = function() { var ret = []; $(this).each(function() { var cur = this.nextSibling; while (cur &amp;&amp; cur != document) { ret.push(cur); cur = cur.nextSibling; }; }); return $(ret); }; </code></pre>
    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.
 

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