Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i get the text in parent element in javascript?
C#
<script language="javascript" type="text/javascript">

var elements= document.getElementsByTagName("span");

alert(elements[5].parent.innerText);

</script>

This is the what i am using, but this code is not returning any alerts.

What property can i use to get the text in parent tag? Please help.
Posted
Updated 21-Aug-13 21:43pm
v3

Try parentNode instead of parent

JavaScript HTML DOM Elements (Nodes)[^]

Here is a common workaround: Find the child you want to remove, and use its parentNode property to find the parent:
JavaScript
var child=document.getElementById("p1");
child.parentNode.removeChild(child);
 
Share this answer
 
Comments
Am Gayathri 22-Aug-13 4:00am    
But i want text in parent. Not in child
and more than 4 childs are there.
thatraja 22-Aug-13 4:11am    
No, actually that's sample code from w3schools.

Try elements[5].parentNode.innerText
Am Gayathri 22-Aug-13 5:40am    
I tried this. But it displaying child text. I want parent text
thatraja 22-Aug-13 5:41am    
Include the HTML part in your question
assign an id to span.
and try this
var a=document.getElementById("span_id");
alert(a.parentElement.innerText)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900