2

I was trying to select everything between adjacent <p>'s, the number of "p" changes every time. And contents in between p tag-pairs could be nothing to anything. Some thing like this:

    <p><a href="x">...ABC...</a></p>

    <<<<<<<< Beginning of what I want >>>>>>>>

    <fieldset>...</fieldset>
    <font title="...">...</font>
    sometext without any tag<br>
    <a href="...">...</a>
    //[0..N] more tags

    <<<<<<<< End of what I want >>>>>>>>

    <p><a href="x+1">...ABC...</a></p>
    [0..N] more "p"'s with similar pattern ("p" with random url in "a")

Update:

I want to wrap those rogue codes (untagged text) into some div so that I can process them later. Like this:

<div id="outer">
    <div id="1">
        <p><a href="x">...ABC...</a></p>

    <! Beginning of what I want >

        <fieldset>...</fieldset>
        <font title="...">...</font>
        sometext without any tag<br>
        <a href="...">...</a>
        //[0..N] more tags

    <! End of what I want >
    </div>
    <div id="2">
        <p><a href="x+1">...ABC...</a></p>
    </div>
    <div id="3">
        //something or nothing
    </div>
    //something or nothing
</div>

In order to do that, I had to this code because there is some text without any tags around it:

    var ps = $("p:contains('ABC')");
    ps.each(function(){
        if(!($(this).next()[0])){
            return true;
        }
        var me = $(this);
        var pa = me.parent().contents();
        var nx = me.next("p:contains('ABC')"); //returns [] in this case
        var i0 = pa.index(me);
        var i1 = pa.index(nx);
        if (i1 > i0) {
            var elements = pa.slice(i0, i1);
            elements.each(function(){
                //Do something
            });
        }
    }); 

As marked in the code, the next() function would not return anything even if I change it to next("p"). But if I use me.next().next().next().next().next() I can select the next "p" tag. Why would this happen? How could I do it better?

Jonas
  • 121,568
  • 97
  • 310
  • 388
ericcire
  • 313
  • 1
  • 6
  • 12

2 Answers2

2

You Should Use .nextUntil("p") for this purpose! because .next() only checks for the next element and nextUntil() searches until an associated element found!

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
1

After reading your question what i am getting is you want the content from <p> tag... whether it is content or html tags. If i am wrong please let me know...

I have created a demo fiddle please have a look. http://jsfiddle.net/dineshswami/YpYWY/1/

Updated Fiddle: http://jsfiddle.net/dineshswami/YpYWY/3/

HTML:

<p><a href="x">...ABC...</a></p>


<fieldset>...</fieldset>
<font title="...">...</font>
sometext without any tag<br>
<a href="...">...</a>
//[0..N] more tags

<p><a href="x+1">...ABC...</a></p>

Jquery:

$("p").contents().unwrap();    

Output:

<a href="x">...ABC...</a>


    <fieldset>...</fieldset>
    <font title="...">...</font>
    sometext without any tag<br>
    <a href="...">...</a>
    //[0..N] more tags

    <a href="x+1">...ABC...</a>
w3uiguru
  • 5,864
  • 2
  • 21
  • 25