SitePoint Sponsor

User Tag List

Results 1 to 25 of 31

Threaded View

  1. #1
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Now here's a puzzler.....!

    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:
    innerText:
    and the other displaying this:
    innerHTML: <b>& pound;</b>
    Right?

    Wrong, unfortunately!!! The first displays as expected, but the second displays this:
    Code:
    innerHTML: <b></b>


    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:
    <b>& pound;</b>
    not this:
    <b></b>
    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!)
    Last edited by M@rco; Jul 13, 2002 at 11:16.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •