[jQuery] Re: Wrapping p siblings following h1 in divs

2009-04-16 Thread Claes H

On Thu, Mar 26, 2009 at 4:01 AM, Ricardo  wrote:
>
> jQuery.fn.nextUntil = function(expr) {
>   var match = [];
>   this.each(function(){
>       for( var i = this.nextSibling; i; i = i.nextSibling ) {
>           if ( i.nodeType != 1 ) continue;
>           if ( jQuery.filter( expr, [i] ).r.length ) break;
>           match.push( i );
>       }
>   });
>   return this.pushStack( match, arguments );
> };
> (plugin by John Resig)
>
> $('h1').each(function(){
>  $(this).nextUntil('h1').wrapAll('div');
> });
>

Thanks for your answer. I missed this answer unfortunately, and first
today returned to this problem. Without seeing your reply I solved it
without jquery as follows:

var headerList = document.getElementsByTagName("h1");
for (var i = 0; i < headerList.length; i++) {
if (i == headerList.length) {
break;
}
var header = headerList[i];
var nextHeader = headerList[i+1];
var nextSibling = header.nextSibling;
var div = document.createElement('div');
div.id = "div-" + header.id;
header.parentNode.replaceChild(div, header);
div.appendChild(header);
while (nextSibling != nextHeader) {
var tmpSibling = nextSibling.nextSibling;
var tmpParent = nextSibling.parentNode;
tmpParent.removeChild(nextSibling);
div.appendChild(nextSibling);
nextSibling = tmpSibling;
}
}

Thanks for taking the time to answer

/Claes


[jQuery] Wrapping p siblings following h1 in divs

2009-03-24 Thread Claes

Hi, I am new to jQuery, and it seems the very first thing I try to do
gives me problems. However, probably I am missing something so let me
ask you if you can hint me on the best way to accomplish this.

I want to transform a document with a flat structure like this

Heading 1
Paragraph 1.1
Paragraph 1.2
Heading 2
Paragraph 2.1
Paragraph 2.2

to something more hierarchical like this

Heading 1

Paragraph 1.1
Paragraph 1.2

Heading 2

Paragraph 2.1
Paragraph 2.2


The biggest problem for me is to detect where the limit between the
paragraphs (in the form of another heading) are. Unfortunately there
is nothing distinctive about the p elements (no ids, no classes, this
is generated code that I can not control)

Thanks!
/Claes