最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

trigger alert on button click with javascript - Stack Overflow

programmeradmin1浏览0评论

I have two textfields with different IDs as shown

<textarea id="textfield">Hello World</textarea>

This will be updated with the content of the first textarea

<input id="messageID">

This is my script

<script type=text/javascript>
function() {

    var value = document.getElementById("textfield").value;
    document.getElementById('#messageID').val(value);
  alert(value);

}              
</script>

This is the onclick button and nothing happens when I click it

<button onclick="myfunction()" type="button" class="btn btn-danger" id="button">Alert</button>

Kindly assist!

I have two textfields with different IDs as shown

<textarea id="textfield">Hello World</textarea>

This will be updated with the content of the first textarea

<input id="messageID">

This is my script

<script type=text/javascript>
function() {

    var value = document.getElementById("textfield").value;
    document.getElementById('#messageID').val(value);
  alert(value);

}              
</script>

This is the onclick button and nothing happens when I click it

<button onclick="myfunction()" type="button" class="btn btn-danger" id="button">Alert</button>

Kindly assist!

Share Improve this question asked Mar 16, 2016 at 22:14 BlazeBlaze 2,34912 gold badges44 silver badges85 bronze badges 5
  • 1 Change .val(value); to .value = value; . Also name your function: function myfunction() { – user4227915 Commented Mar 16, 2016 at 22:17
  • Try to name your function myfunction(){ var value = document.getElementById("textfield").value; document.getElementById('#messageID').val(value); alert(value);} – Jorge Mejia Commented Mar 16, 2016 at 22:17
  • Nothing happens still – Blaze Commented Mar 16, 2016 at 22:19
  • Remove the sharp # from document.getElementById('#messageID') – user4227915 Commented Mar 16, 2016 at 22:20
  • Try something like this: jsfiddle/jorge182/x7av4m96 – Jorge Mejia Commented Mar 16, 2016 at 22:26
Add a ment  | 

4 Answers 4

Reset to default 1

Try something like this:

function myfunction() {

  var value = document.getElementById("textfield").value;
    document.getElementById('messageID').value=value;
  alert(value);

}  
<input type="button" value="Alert" onclick="myfunction()" type="button" class="btn btn-danger" id="button"/>
<textarea id="textfield">Hello World</textarea>

<input id="messageID">

Three things I'm seeing wrong:

  1. .val(value); is a jQuery' method, not javascript... you should change it to .value = value;

  2. to call onclick="myfunction()" you should name it: var myfunction = function(){

  3. The document.getElementById() method doesn't need sharp # before the name.

Hope it helps.

The most important catch is whenever you declare function on button click you should define that function inside javascript.

<script type=text/javascript>
function myfunction() {

    var value = document.getElementById("textfield").value;
    document.getElementById("messageID").value = value;
    alert(value);

}              
</script>
<textarea id="textfield">Hello World</textarea>
<input id="messageID">
<button onclick="myfunction()" type="button" class="btn btn-danger" id="button">Alert</button>

Here you go a working fiddle

https://jsfiddle/blazeeboy/fNPvf/

Its inner Html you are trying to get

<textarea id="textfield">Hello World</textarea>
<input id="messageID"/>
<button type="button" class="btn btn-danger" id="button"    onclick="myfunction()">Alert</button>

function myfunction(){
    alert(1);
    var v = document.getElementById("textfield").innerHTML ;
   document.getElementById('messageID').innerHTML = v;
  alert(v);
}
发布评论

评论列表(0)

  1. 暂无评论