Change input field value onclick
I am trying to change the input value using the innerHtml button of the button that is clicked ... I tried several ways but none worked
<script>
function changeValue(){
var cValue = document.getElementbyId('technician').innerHTML;
var cType = document.getElementbyId('type');
var cType.value = cValue;
}
</script>
<button id="technician" onclick="changeValue()">Technician</button>
<input type="" id="type" name="type" value="change"></input>
I have also tried
<script>
function changeValue(){
var cValue = document.getElementbyId('technician').innerhtml;
document.getElementbyId('type').value = ('cValue');
}
</script>
none of them work
+3
source to share
5 answers
you have a few typos in your code uppercase and lowercase letters matter in things like getElementById and innerHTML
I believe this is what you are trying to do:
<script>
function changeValue(o){
document.getElementById('type').value=o.innerHTML;
}
</script>
<button id="technician" onclick="changeValue(this)">Technician</button>
<button id="developer" onclick="changeValue(this)">Developer</button>
<input type="text" id="type" name="type" value="change" />
+5
source to share
Here's a REALLY simple way to do it:
function changeValue(value) {
document.getElementById('button1').innerHTML = value;
}
<button onclick="changeValue('This content has changed.')" id="button1">I have some content that will change when you click on me.</button>
I hope this example helps!
+1
source to share
I think this is what you want:
<script>
function changeValue(){
document.getElementById('technician').innerHTML = document.getElementById('type').value;
}
</script>
<button id="technician" onclick="changeValue()">Technician</button>
<input id="type" name="type" value="change"></input>
0
source to share