Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Text of Nested Span HTML element?

I want to get text from Nested SPAN element in Following HTML code:

<span id='result_box'>
<span class="hps">text_content</span>
</span>

I want to get "text_content" value using JavaScript.

I have tried this but have a problem:

var resBox=document.getElementById('result_box');
var strTrans=resBox.getElementsByTagName('span')[0].innerHTML;
alert(strTrans);

EDIT: Actually i want to do this from Online Page enter image description hereenter image description here

like image 295
Ganpat Avatar asked Dec 24 '12 07:12

Ganpat


1 Answers

your code works fine. i guess your problem is you are executing these code when DOM not loaded completely.if you are testing something, you can try this.

window.onload = function () {
   //put your code here,it will alert when page loaded completely.
}; 

or put the script after your span element. like this.

<span id='result_box'>
  <span class="hps">text_content</span>
</span>
<script type='text/javascript'>
   var resBox=document.getElementById('result_box');
   var strTrans=resBox.getElementsByTagName('span')[0].innerHTML;
   alert(strTrans);// it will alert
</script>
like image 142
chris Avatar answered Oct 20 '22 01:10

chris