Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change value of input field onclick

I am trying to change the value of an input value with the innerHtml of the button that is clicked ... I have tried a couple of ways but none have 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 also tried

<script>
   function changeValue(){
     var cValue = document.getElementbyId('technician').innerhtml;
     document.getElementbyId('type').value = ('cValue');
    }
</script>

neither seems to be working

like image 392
tru Avatar asked Dec 14 '25 06:12

tru


2 Answers

you have several typos in your code uppercase and lowercase letters do matter in things like getElementById and innerHTML

i believe this is what you're 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" />
like image 107
barney.tearspell Avatar answered Dec 16 '25 20:12

barney.tearspell


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 that this example helps!

like image 28
Drazzah Avatar answered Dec 16 '25 18:12

Drazzah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!