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
#
fromdocument.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
4 Answers
Reset to default 1Try 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:
.val(value);
is a jQuery' method, not javascript... you should change it to.value = value;
to call
onclick="myfunction()"
you should name it:var myfunction = function(){
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);
}