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" />
      

Run codeHide result


+5


source


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>
      

Run codeHide result


I hope this example helps!

+1


source


<button id="technician" onclick="changeValue()">Technician</button>
<input type="" id="type" name="type" value="change"></input>
<script>
   function changeValue(){
        document.getElementById('type').value="There you go";
    }
</script>

      

0


source


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


Thanks for all your answers. My problem was that when I called var cVaue I nested it

document.getElementbyId('type').value = ('cValue');

      

When I need code like this

document.getElementbyId('type').value = cValue;

      

0


source







All Articles