innerhtml - Showing the contents of a SPAN using JavaScript - Stack Overflow

I have this code. I've done this for years now but I'm stumped with the result of this exampl

I have this code. I've done this for years now but I'm stumped with the result of this example. The purpose is to make the text box visible and put the contents of the clicked SPAN tag in it.

document.onclick = CaptureClickedElement;

function CaptureClickedElement(e)
{
var EventElement;
if(e==null)
    EventElement = event.srcElement;// IE
else
    EventElement = e.target;// Firefox

if( EventElement.tagName == "SPAN")
    {
    document.getElementById("divTXT").style.display="";
    document.getElementById("txt").value = document.getElementById("Span1").innerHTML;
    alert(document.getElementById("Span1").innerHTML)
    }
}

Strangely though, it DOES show the contents but also shows open/close SPAN tags at the end of it. If I alert ther results, the same thing is shown.

Please find the attached screen shot of it here.

Does anyone have an idea of why this is happening?

Thanks!

Here is the HTML (copied from ments by mplungjan)

<style type="text/css"> 
 #divOuter { 
   width: 100px; 
   height: 70px; 
   border: 1px solid blue; 
   float: left; 
 } 
 </style>

<body> 
  <div> 
    <form name="frm" method="post" action=""> 
      <div id="divTXT" style="display:none"> 
        <input type="text" id="txt" name="txt" value="" size="30" /> 
      </div> 
    </form> 
  </div> 
  <div id="divOuter"> 
    <span id="Span1">hi, this is a test.<span> 
  </div> 
</body>

I have this code. I've done this for years now but I'm stumped with the result of this example. The purpose is to make the text box visible and put the contents of the clicked SPAN tag in it.

document.onclick = CaptureClickedElement;

function CaptureClickedElement(e)
{
var EventElement;
if(e==null)
    EventElement = event.srcElement;// IE
else
    EventElement = e.target;// Firefox

if( EventElement.tagName == "SPAN")
    {
    document.getElementById("divTXT").style.display="";
    document.getElementById("txt").value = document.getElementById("Span1").innerHTML;
    alert(document.getElementById("Span1").innerHTML)
    }
}

Strangely though, it DOES show the contents but also shows open/close SPAN tags at the end of it. If I alert ther results, the same thing is shown.

Please find the attached screen shot of it here.

Does anyone have an idea of why this is happening?

Thanks!

Here is the HTML (copied from ments by mplungjan)

<style type="text/css"> 
 #divOuter { 
   width: 100px; 
   height: 70px; 
   border: 1px solid blue; 
   float: left; 
 } 
 </style>

<body> 
  <div> 
    <form name="frm" method="post" action=""> 
      <div id="divTXT" style="display:none"> 
        <input type="text" id="txt" name="txt" value="" size="30" /> 
      </div> 
    </form> 
  </div> 
  <div id="divOuter"> 
    <span id="Span1">hi, this is a test.<span> 
  </div> 
</body>
Share Improve this question edited May 1, 2011 at 5:57 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked May 1, 2011 at 5:42 itsolsitsols 5,5927 gold badges56 silver badges97 bronze badges 2
  • <style type="text/css"> #divOuter { width: 100px; height: 70px; border: 1px solid blue; float: left; } </style> – itsols Commented May 1, 2011 at 5:51
  • <body> <div> <form name="frm" method="post" action=""> <div id="divTXT" style="display:none"> <input type="text" id="txt" name="txt" value="" size="30" /> </div> </form> </div> <div id="divOuter"> <span id="Span1">hi, this is a test.<span> </div> </body> – itsols Commented May 1, 2011 at 5:52
Add a ment  | 

2 Answers 2

Reset to default 3

Structure problem:

<span id="Span1">hi, this is a test.<span>

Note the absence of a proper close to the span.

Need to use .innerText (for IE) and .text for others.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745620615a4636495.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信