Alert function is not running when clicking the button

Asked

Viewed 86 times

-2

By typing the text in input and click on the button, the function alert() does not perform!

My code:

    <html lang="pt">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
	<h1>Teste Javascript</h1>
	<div id="app">
		<input type="text" name="nome" />
		<input type="text" name="nome2" />
		<button class="botao">Pressione aqui</button>
	</div>
	<script>
		var inputElement = document.querySelector('input[name=nome2]');
		var btnElement = document.querySelector('button.botao');
               btnElement.onClick = function(){
			      var text = inputElement.value;
			      alert(text);
		          }
    </script>
    </body>
    </html>

What’s the matter?

3 answers

1

0


The nomenclature this wrong, change its function to btnElement.onclick = function () {...

Another way to work would be to create a Istener

btnElement.addEventListener('click', function() {
  função...
});

Source: https://www.w3schools.com/jsref/event_onclick.asp

  • 1

    But it’s exactly the same answer that was posted an hour ago

  • 1

    My intention was to show you another way to work.

  • 1

    This other way has already been mentioned in my other reply. So much so that I recommended reading it. xD

-2

 
  $(document).on("click",".botao",function() {
		var input1 = $('input[name="nome"]').val();
		alert(input1);
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="nome"/>
<button type="button" class="botao">Ok</button>

  • Try to explain your code do

Browser other questions tagged

You are not signed in. Login or sign up in order to post.