0
<div>
    some text
    <img />
    <br>
    text I want to select
    <br>
    <img />
    some text
</div>

The HTML is like above, I want to select the text between the two images, is there anyway to do this?

Some background: I am trying to format some poorly written HTML so it can be further stylized. There are too many pages of them to hardcode it one by one, they have some certain pattern though, so I am trying to write a javascript function to make the whole procedure easier. The text between <br> is actually the description of the first image, I want to wrap it with <div> so I can add class to it and stylize it. But first I need to select it, right?

shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • What have you tried ? Seems trivial enough.. Also how is the text generated ? – Pogrindis Aug 05 '15 at 14:45
  • @Pogrindis it is trivial.... doesn't even seem like he put any effort in the solution http://stackoverflow.com/questions/26897762/get-text-between-two-images – Huang Chen Aug 05 '15 at 14:47

2 Answers2

1

Simplest solution using core jQuery functions:

$('img').nextUntil('img').doSomething();

Jonathan Snook pointed me to the jQuery 1.4 function that solves this:

Check out it's sibling functions:

Inspired by:

For other reference check this one :How to select all content between two tags in jQuery

Community
  • 1
  • 1
Kaushik Thanki
  • 3,334
  • 3
  • 23
  • 50
  • jQuery's traverse function seems to be skipping textnodes. However, vanilla javascript's nextSibling can do the job. Is there anything in jQuery that can ahieve the same thing with .nextSibling? – shenkwen Aug 05 '15 at 18:51
0
<div>
    some text
    <img />
    <br>
    <span>text I want to select <span>
    <br>
    <img />
    some text
</div>

add tag for this and get "div span" . If have more span in "div" . add id or class for them.

Cyclone
  • 5
  • 3