Valerie Meunier

Valerie Meunier

  • 970
  • 693
  • 72.6k

how to pass value of input to function of onclick

Aug 28 2022 6:41 PM

Hi

I try to pass the value from the input to the function that is triggered by event onclick of the button. Finally, when clicking on the image, the Alert must appear. If i type value 3 in the input, the Alert must show 3. But it doesn't work.

Thanks.

<img id="clem" src="fotos/clem.jpg" />
<input id="Text1" type="text" />

<input id="Button1" type="button" value="button" onclick="myFunction()" />

<script type="text/javascript">
        function myFunction() {
            var a = document.getElementById("Text1").value;
            document.getElementById("clem").onclick = 'go(" + a + ")';
        }
    </script>

    <script type="text/javascript">
        function go(z) {
            alert(z);
        }
    </script>

 


Answers (2)