Javascript onClick function - copy text to input value - Stack Overflow

I need to change the value displayed in a form. For example:<span id="copy" onClick="

I need to change the value displayed in a form. For example:

<span id="copy" onClick="addValue()">**Text1**</span>
<span id="copy" onClick="addValue()">Text2</span>

Based from what I click, I want to move it to the input value like this:

<input id="paste" class="text" type="input" name="select" value="**Text1**"/>

Function

<script language="javascript" type="text/javascript">   
    <!--Hide Javascript         
      function addValue(){
         document.getElementById('paste').value = this.innerText;       
     }  
 -->  </script>

When I click on Text1 in value fild I get "undefined" instead of "Text1" ...

I need to change the value displayed in a form. For example:

<span id="copy" onClick="addValue()">**Text1**</span>
<span id="copy" onClick="addValue()">Text2</span>

Based from what I click, I want to move it to the input value like this:

<input id="paste" class="text" type="input" name="select" value="**Text1**"/>

Function

<script language="javascript" type="text/javascript">   
    <!--Hide Javascript         
      function addValue(){
         document.getElementById('paste').value = this.innerText;       
     }  
 -->  </script>

When I click on Text1 in value fild I get "undefined" instead of "Text1" ...

Share Improve this question edited Aug 13, 2022 at 22:50 DevWL asked Aug 6, 2013 at 23:26 DevWLDevWL 18.9k6 gold badges97 silver badges92 bronze badges 2
  • 1 Please explain this a little more. I don't know what is moving where, or what is being changed, when, or why. – user2625787 Commented Aug 6, 2013 at 23:28
  • 1 Please discuss the research you have done on this before asking the question. Showing code you have tried would be great. – user2625787 Commented Aug 6, 2013 at 23:29
Add a ment  | 

3 Answers 3

Reset to default 1

Give this a go.

You need to pass the element into your function so you can access it's properties to assign them to your form input.

HTML:

<form name="myForm">
    <span onclick="copyText(this)" >Text1</span>, <span onclick="copyText(this)" >Text2</span>
    <br>
    <input name="myField"></input>
</form>

Javascript:

function copyText(element) {
   document.myForm.myField.value = element.innerHTML;
}

I'd use:

 function addValue(el) {
     document.getElementById('paste').value = el.innerText || el.textContent || el.outerText;
 }

And set the HTML tag like so:

 <span id="copy" onClick="addValue(this)">**Text1**</span>
document.myForm.myField.value = "I'm a different value now!";

Is this what you're looking for?

Form and input should have a "name" property, like:

<form name="myForm">
    <input name="myField" />

in the case above.

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

相关推荐

  • Javascript onClick function - copy text to input value - Stack Overflow

    I need to change the value displayed in a form. For example:<span id="copy" onClick="

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信