@Michel

Yes, basically you're totally right. Your script is much nicer and
does not nest loops.
Logic is not exactly as it should be.

The idea is:

After each closing h3 tag (</h3>) inside modAccordion container append
an opening div (<div>) tag.
Before every opening h3 tag (<h3>) but not before the first opening h3
tag add a closing div (</div>) tag.
Finally before the closing div tag from the modAccordion container,
add a closing div (</div>) tag.

But you got me close!

Thanx anyway, I appreciate your help.

Regards,

hagbard (aka Boris)

On 19 Nov., 10:14, Michel Belleville <michel.bellevi...@gmail.com>
wrote:
> If I may be so bold, perhaps you'd be better off using iterators and avoid
> nesting while loops based on the same numeric condition.
>
> For exemple, let's imagine I'd like to round up all h3 nodes and their
> immediate following siblings in neat <div>s here's what I'd do :
>
> $(".modAccordion > h3").each(function() {
> var switch = true; // will be used in the select iteration to stop the
> selection when reaching the next h3 element
> $(this).nextAll() // selects all the next siblings
> .select(function() { return switch && (switch = ! $(this).is('h3')); }) //
> stops the selection when the next h3 is reached
> .wrapAll('<div class="new"></div>'); // wrap all remaining (the immediate
> next siblings before encountering the next h3) in a div with class="new"
>
> });
>
> This should be less messy than nested whiles, while doing the same thing if
> I read your method right.
>
> Michel Belleville
>
> 2009/11/19 hagbardceline <hagbardcelin...@gmail.com>
>
> > I have the following script (see below), which dynamically adds div
> > between closing and opening h3 tags.
> > The script works fine in Firefox but IE is infinite looping the first
> > while.
>
> > Does anyone have an idea how to fix the script for IE?
>
> > Thx!
> > hagbard
>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> >  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> > <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en-us">
> > <head>
>
> > <meta http-equiv="content-type" content="text/html; charset=utf-8" />
> > <script type="text/javascript"
> > src="http://tablesorter.com/jquery-latest.js";></script>
> > <script type="text/javascript">
>
> > jQuery(function(){
>
> >   var nodes = new Array();
> >   nodes = $(".modAccordion").children();
> >   $(".modAccordion").children().remove();
>
> >   var i = 0;
> >   var headerTest = /.(Head)./;
>
> >   while(i < nodes.length)
> >   {
> >      var testNode = nodes[i].toString();
>
> >      if((testNode.match(headerTest))){
> >         $(".modAccordion").append(nodes[i]);
> >         i++;
>
> >         var newDiv = document.createElement("div");
> >         newDiv.className="new";
>
> >         while(i < nodes.length && !((nodes[i].toString()).match
> > (headerTest)))
> >         {
> >            newDiv.appendChild(nodes[i]);
> >            i++;
> >         }
> >         $(".modAccordion").append(newDiv);
> >      }
>
> >   }
>
> > });
>
> > </script>
> > <style>
> > div.new
> > {
> >   background: #00ff00;
>
> > }
>
> > .modAccordion
> > {
> >   background: #ff0000;
>
> > }
>
> > </style>
> > <title>TableSorter</title>
> > </head>
> > <body>
> > <div class="modAccordion">
> >  <h3>header 1</h3>
> >   <p>some text</p>
> >   <p>some other text</p>
> >  <h3>header 2</h3>
> >   <p>some text</p>
> >   <ul>
> >     <li>list element 1</li>
> >     <li>list element 2</li>
> >   </ul>
> >  <h3>header 3</h3>
> >   <p>some text</p>
> >   <p>some other text</p>
> >   <p>some more text</p>
> >   <p>some different text</p>
> > </div>
> > </body>
> > </html>

Reply via email to