Javascript help

Asked

Viewed 72 times

-2

<!DOCTYPE html>
<html>
<head>
	<title>Exercicio 3</title>
	<meta charset="utf-8">
</head>
<body>
	<center><h1>Números dos Meses</h1>
	<input type="number" name="numeros" max="12" min="1" id="mes">
	<p id=></p>
	<input type="submit" onchange="mes()">
	<script type="text/javascript">
		function mes(){
		let mes = document.getElementById('mes').value;
		switch (mes) {
			case 1:
				document.getElementById('mes').innerHTML = "Janeiro";
			break;
			case 2:
				document.getElementById('mes').innerHTML = "Fevereiro";
			break;
			case 3: 
				document.getElementById('mes').innerHTML = "Março";
			break;
			case 4:
				document.getElementById('mes').innerHTML = "Abril";
			break;
			case 5: 
				document.getElementById('num').value;
				document.getElementById('mes').innerHTML = "Maio";
			break;
			case 6:
				document.getElementById('mes').innerHTML = "Junho";
			break;
			case 7:
				document.getElementById('mes').innerHTML = "Julho";
			break;
			case 8:
				document.getElementById('mes').innerHTML = "Agosto";
			break;
			case 9:
				document.getElementById('mes').innerHTML = "Setembro";
			break;
			case 10:
				document.getElementById('mes').innerHTML = "Outubro";
			break;
			case 11:
				document.getElementById('mes').innerHTML = "Novembro";
			break;
			case 12:
			document.getElementById('mes').innerHTML = "Dezembro";
			break;
		}
	}
	</script>

</center>
</body>
</html>

I need to do this exercise : ** Enter the month number and print the month name. (switch)** But my code is not working the error appears on line 17 says that it is a syntax error and that I did not expect an Identifier I assume there is something else wrong someone can kindly show the way ? (OTHER ERROR) Does my button onclick not work what I do? (SOLVED)

  • After the case X should not have the two points?

  • switch (on =! 0) switched to switch(on) ?

  • yes I already changed I will reset to see

2 answers

1

You didn’t put the input and not the button on a form. Try to do so:

let form_mes = document.getElementById("form_mes");
form_mes.addEventListener('submit', function() {
  let mes = document.getElementById("num").value;
  let mes_name = document.getElementById("mes");
  switch (mes) {
    case "1":
      mes_name.innerHTML = "Janeiro";
      break;
    case "2":
      mes_name.innerHTML = "Fevereiro";
      break;
  }
});
<form id="form_mes" onsubmit="return false;">
  <h1>Número dos Meses</h1>
  <input type="number" name="num" id="num" min="1" max="12">
  <p id="mes"></p>
  <button type="submit">Enviar</button>
</form>

0

Missing two points ( : ) after each case

switch (num) {
  case 1:
    document.getElementById('mes').innerHTML = "Janeiro";
    break;
}

  • the error is gone but nothing appears in the onclick button

Browser other questions tagged

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