user3847060
user3847060

Reputation: 141

how to get text outside of a span

I'm trying to get text outside of a span but I can only get the text inside the span.

<span class="text"> some text </span> text I want

how can I get the text "text I want"

I'm using the jQuery but it only gets "some text"

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

Upvotes: 5

Views: 2724

Answers (4)

Deathmras
Deathmras

Reputation: 312

You have to use that on parent of span. http://jsfiddle.net/euvKK/

$(document).ready(function() {
    var a = $(".parent").contents().filter(function() {
        return this.nodeType == 3;
    }).text();
    alert(a);
});

Upvotes: 3

kyr
kyr

Reputation: 39

$('.parentClass:not(span)').method

This will get everthing except the span, if that was what you were looking for.

Upvotes: 1

celerno
celerno

Reputation: 1387

This will give you all text that is not inside an element.

http://jsfiddle.net/4QZGz/1/

HTML

<div>
    <span class="text"> some text </span> text I want
     <br/>
    <div id="here"></div>
</div>

JS

a = $('.text');


$('#here').html(textAfter(a));

function textAfter(obj){
    var p=$(obj).parent();

//    alert(/<\/\w+>(\s*[\w+\s+]+)<\w+>/.exec(p.html()));
    return p.html().match(/<\/\w+>(\s*[\w+\s+]+)<\w+>/)[1];

}

Upvotes: 0

Kalpesh Rajai
Kalpesh Rajai

Reputation: 2056

try thi one...

<div id="foo"><span>hello</span>i want</div>

$(document).ready(function(){
   $value=$("#foo")
    .clone()    //clone the element
    .children() //select all the children
    .remove()   //remove all the children
    .end()  //again go back to selected element
    .text();
    alert($value);
});

js fiddle http://jsfiddle.net/qrASy/ thank you...

Upvotes: 2

Related Questions