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.]

2
  • probably innerText is what you are looking for. Commented Dec 22, 2014 at 8:17
  • Why are you using such complicated script? Can you show a snipet of the page? Commented Dec 22, 2014 at 8:33

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.