0

Possible Duplicate:
Get the text after span element using jquery

I am trying to select the text next to a span element.

I have

    <span class='spanClass'>span text</span>

test this string....

I used

$('.spanClass').next().val() ->give me undefined....

//how to get the 'test this string....'?

Thanks for the help.

Community
  • 1
  • 1
Rouge
  • 4,181
  • 9
  • 28
  • 36
  • 1
    check http://stackoverflow.com/questions/6925088/get-the-text-after-span-element-using-jquery out – Breezer Sep 11 '12 at 22:50

3 Answers3

0

You need to use

<div>
    <span class='spanClass'>span text</span> 
    <label>Hello World!!</label>
</div>

   $('.spanClass').next().text();

            OR

   $('.spanClass').next().html();

.val() is used for input elements.. .text() is for labels and divs

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

Working demo bit different :) http://jsfiddle.net/4Rx2r/

Also you can chuck that in your js file as a plugin :) if you want.

Hope it fits the need :)

Code

jQuery.fn.justtext = function() {

    return $(this).clone()
            .children()
            .remove()
            .end()
            .text();

};


alert($('.spanClass').parent().justtext());
​
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
0

Get the content of the parent element and filter for textnodes:

var text = $('.spanClass').parent().contents().filter(function() {
    return this.nodeType===3;
});

FIDDLE;

adeneo
  • 312,895
  • 29
  • 395
  • 388