0

HTML:

<div id ="split"><p>hi one two three</p><img class ="some class" src="some src" master_src="src" master_w="318" master_h=22"><img class="some" src="src"><h1> 4 5 6</h1></div>

Desired output:

<p>hi one two three</p>
""           
<h1>4 5 6</h1>

How can i get this output ? I wanted a general form if there can be n images inside a div.

$("#split").find('img').each(function(index){
       alert($(this).splitsuchthat()) 
// for first time it should be <p>hi one two three</p>
// secon time ""
// third time <h1> 4 5 6</h1>  

});
James Montagne
  • 77,516
  • 14
  • 110
  • 130
Bad Coder
  • 866
  • 1
  • 12
  • 27

2 Answers2

1

You can use prevUntil (or nextUntil) to get the element between two images. Then you simply need to deal with the remain end:

http://jsfiddle.net/GLZaJ/1/

// a helper function to print the html of elements contained in a jquery object
function toHtmlString($els){
    var clone = $els.clone();

    return $('<div>').append(clone).html();
}

// get all elements before an image
$("#split > img").each(function(index){
    var $prevs = $(this).prevUntil("img");
    var htmlString = toHtmlString($prevs);
    alert(htmlString);
});

// get everything after the last image
var $after = $("#split > img:last").nextAll();
var htmlString = toHtmlString($after);
alert(htmlString);
James Montagne
  • 77,516
  • 14
  • 110
  • 130
-1

I am not sure if I understand your question but here is maybe what you need:

$(function(){
    $("#split>*").each(function(index){
                     alert($(this).text()) ;
                     });

});

jsfiddle: http://jsfiddle.net/9Z5Mt/

MilMike
  • 12,571
  • 15
  • 65
  • 82