How to get the text up #target_element? ...">

How to get text before $ el using jQuery?

<cite>
    text here...
    <a id="target_element"></a>
</cite>

How to get the text up #target_element?

+3
source share
3 answers
$("#target_element").parent().contents().filter(function(){ 
       return this.nodeType == 3; 
}).text();

demo

+3
source

This is not a pleasant solution, but it will work in this particular case:

$('#target_element').parent().text();

It is best to put this text in another element, such as a span. Otherwise, this text can be easily confused. If this were in between, you could do something like:

$('#target_element').prev().text();

Thus, you will encounter fewer errors.


EDIT

I found in another way :

var elementClone = $('#target_element').clone();
elementClone.children().remove();
elementClone.text();
+2
source

for the first time take a place for a replacement:

<div id="target"></div>
<a id="target_element"></a>
0
source

Source: https://habr.com/ru/post/1751534/


All Articles