StealthRT StealthRT - 3 months ago 13
jQuery Question

Finding previous tag name using jQuery Prev()

Hey all I am using the following code to go back 1 in order to find the start of the tag name:

console.log($('text[text-anchor=\'middle\']').prev().prop("tagName"));


This produces this result:


text


However, that is incorrect since it should be < g>
enter image description here

If I do this:

console.log($('text[text-anchor=\'middle\']').prev())


I get this:

enter image description here

So I know its getting the correct area but how can I go about getting the < g>?

Answer

g tag is parent of text elements. You need to use .parent() or .closest() selector for traversing to parent element:

$('text[text-anchor=\'middle\']').parent()

or

$('text[text-anchor=\'middle\']').closest('g')