19

Let's consider following html code:

<p>
  Some text followed by <span>a span element</span>
  and another text followed by <b>a bold text</b>.
</p>

How can I get the text before the span and b elements using jQuery?

I tried $("span").prev() and $("b").prev() but it did work because the text is not an element. I also tried $("span").parent() but it matched whole paragraph, while I want only a part of it.

Could you advise any solution? Thank you in advance :-)

2
  • pretty much duplicate of: stackoverflow.com/questions/1017330/… Commented Oct 9, 2009 at 13:15
  • 3
    Not so similar. Here I want to extract only the text appearing directly before given element, not whole text. Commented Oct 9, 2009 at 13:30

4 Answers 4

27

How about collecting them into an array instead of two calls to $:

var texts = $('span, b').map(function(){
    return this.previousSibling.nodeValue
});
texts[0]; // "Some text followed by "
texts[1]; // " and another text followed by "

References:

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the solution. However, I cannot make it work in this scenario. var c = jQuery('span'); and later c.previousSibling returns null but c.context.previousSibling is ok. Why?
I'm saying, don't use that scenario (c.context). Use the code in my answer.
4
var textBefore = $('span')[0].previousSibling.nodeValue;

I was looking for a solution to this, and was confused why I needed to map to an array. Context was not working for me. I finally figured out how to write it.

This is the most basic solution. Putting the [0] converts the element to an elementNode, which allows you to call the previousSibling code. There's no need to put it in an array using the map function unless it is more convenient.

Comments

3

I'm afraid you will need to use regular expressions for that. In order to get the text before span element use something like this:

var txt = $('span').parent().html();
var regex = /(.*)<span>/;
txt = txt.match(regex)[1];

Modify the regex to match other parts of the string as well.

2 Comments

This won't work because text() doesn't return any html. Secondly, even if you change it to using html(), the target element may not be the first span in the parent.
This is just a demo how you can do it. There's no easy way to get this text stripping all tagged contents. You have to use regular expressions.
0

I have come up with a following solution:

var c = $("span").context.previousSibling;
alert(c.data);

But I am not sure how safe it is when using different browsers?

2 Comments

Why don't you just use my solution? It does the same thing but without accessing .context.
This is an alternative solution. Your solution cannot be used (I could not) in all situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.