3

I have the below html markup

<div class="title-wrap">
      <span class="title">Hello World</span>
        <br/>
   This title is about Hello World
</div>

and I want to target the text 'This title is about Hello World'

But I can't work out how to do this with out also including the span? Is it possible to achieve this with the html in this format?

I have tried using

var test = (jQuery('.title-wrap').text() || '' );
alert (test);

but this returns

Hello World

This title is about Hello World

as shown in this fiddle.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
ak85
  • 4,154
  • 18
  • 68
  • 113

3 Answers3

3

You can use this:

var test = (jQuery('.title-wrap')
        .clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text() || '' );
alert (test);

Output:

This title is about Hello World

Fiddle: http://jsfiddle.net/Y6dcU/6/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
3

You can use .contents() to get all the child nodes of .title-wrap then use .last() to select the last one since that's the one you want.

jQuery('.title-wrap').contents().last().text()

http://jsfiddle.net/Y6dcU/11/

Musa
  • 96,336
  • 17
  • 118
  • 137
-2

Try wrapping it in a span element. You can use a header(h1, h2, etc) to substitute the first span, or you can use an id or class attribute to get a specific reference:

<div class="title-wrap">
    <span class="title">Hello World</span>
    <br>
    <span id="myTitle">This title is about Hello World<span>
</div>

In JS you would use:

var test = (jQuery('#myTitle').text() || '' );
alert (test);
Musa
  • 96,336
  • 17
  • 118
  • 137
Reydel Leon
  • 1,035
  • 2
  • 11
  • 34