1

In the following code I am extracting the innerHTML text of the html element.

if(a.tagName){
if("option"==a.tagName.toLowerCase())
    return a.text.replace(/\u00A0/g," ");
if("select-one"==a.type||"select-multiple"==a.type)
    return this.getSelectBoxText(a,!1);
}
if(this._isIE()||this.isSafariLike()&&!this._isChrome())
    return a.innerText||a.textContent||"";
var b=a.innerHTML;
return!b||-1==b.indexOf("\x3cbr")&&-1==b.indexOf("\x3cBR")?a.textContent:document.createElement?  (b=document.createElement(a.tagName),b.innerHTML=a.innerHTML.replace(/<br[\/]*>/ig," "),b.textContent):a.textContent
};

above code return the correct innerHTML text for all HTML element. There is an issue when any HTML element contain text with special character code e.g.
<a id="oopID1" href="...">OOP &ndash; Java</a> [in page is show "OOP - Java"].

then it does not return the actual rendered text (mean "OOP - Java").

How could I get the actual value which is getting displayed in the page.

Thanks in advance.

[NOTE : I don't want to use jQuery.]

BhushanK
  • 1,205
  • 6
  • 23
  • 39

2 Answers2

3

Use textContent to retrieve the text content of your element:

document.getElementById("oopID1").textContent

see fiddle HERE

Useful links:

textContent documentation

innerHTML documentation

innerText vs innerHTML

Community
  • 1
  • 1
BeNdErR
  • 17,471
  • 21
  • 72
  • 103
2

Use innerText rather than innerHTML as the former gets the value as text, and the latter gets it as markup.

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22