It is with great satisfaction that I at last post the
first problem that I haven't been able solve myself(!!!), and simultaneously reach my 800th post!
I am trying to get retrieve the inner HTML (i.e. the HTML in between the <a> and </a> tags) of an anchor tag when I click on the link. This should be easy enough to do using the .innerHTML property, and indeed it is most of the time, but there appears to be an exception when it comes to character entities (e.g. "& pound;" - please note that spaces have been intentionally inserted where needed throughout this post in order to overcome the usual shortcomings in vB's handling of HTML code)
At this point I should probably point out that in this particular case I'm not concerned with cross-browser compatibility - I only need this to work in IE4 and up.Here's some simple test code to demonstrate the problem:
Code:
<script language="JavaScript"> function DisplayValue(obj) { alert("innerText: " + obj.innerText); alert("innerHTML: " + obj.innerHTML); }</script><a title="Pound sterling" onClick="DisplayValue(this);" href="#"><b>& pound;</b></a>So, in theory, when the pound sign is clicked on, two alert boxes should appear - one displaying this:
and the other displaying this:
Quote:
| innerHTML: <b>& pound;</b> |
Right?
Wrong, unfortunately!!! The first displays as expected, but the second displays this:
So, the question is, how can I obtain the _exact_ HTML that is present between the tags via JavaScript? If .innerHTML doesn't work then what does?
For total clarification, what I want is this:
not this:
Also, I am well aware that I could do something like this:
Code:
<script language="JavaScript"> function DisplayValue(obj,val) { alert("innerText: " + obj.innerText); alert("innerHTML: " + val); }</script><a title="Pound sterling" onClick="DisplayValue(this,'<b>& pound;</b>');" href="#"><b>& pound;</b></a>but although this works, it is an unnecessary duplication, and not what I want.
You would have thought that returning the unparsed HTML code from the contents of an element would be pretty elementary (pun intended), but it seems that it is not! It
must be possible to do such a basic thing!
So, answers on a postcard to the usual address.....!
(Thinking caps on, everyone!)