Switch javascript always falling in default [closed]

0

I'm studying js, I want to do a seconds converter in minutes and hours.

I was doing a test to see if everything is working 100% and I've come across a problem. This is almost an hour ago breaking the head. I would like someone to point me where I'm going wrong.

function timesquare()
	{
		var optionsTime = document.getElementById('optionsTime').value;
		var convert = document.getElementById('convert').value;


		if (convert < 60) {
			document.getElementById('resultTime').innerHTML = "Escolha um valor acima de 60";
			return;
		}

		switch(optionsTime){
			case "minutes":
				document.getElementById('resultTime').innerHTML = 'Vc escolheu minutos';
				break;
			case "hours":
				document.getElementById('resultTime').innerHTML = 'Vc escolheu Horas';
				break;

			default:
				document.getElementById('resultTime').innerHTML = "no result";
			break;
		}
	}
<input type="text" id="convert" placeholder="Insira os segundos">

<br><br>

<select id="optionsTime">
	<option id="minutes">Minutos</option>
	<option id="hours">Horas</option>
	<option id="teste">Teste</option>
</select>

<br><br>

<button onclick="timesquare();">Converter</button>

<br><br>

<span id="resultTime"></span>
    
asked by anonymous 05.08.2017 / 01:56

1 answer

1

In addition to the MarceloBoni suggestion "nos options do select, troque id para value..."

You can also run the code with ids in the options. See

  • Example 1

document.getElementById("converter").onclick = function() {
timesquare(document.getElementById("optionsTime"));
};

function timesquare(s) {

var optionsTime = (s[s.selectedIndex].id);
var convert = document.getElementById('convert').value;

  if (convert < 60) {
document.getElementById('resultTime').innerHTML = "Escolha um valor acima de 60";
return;
  }

  switch(optionsTime){
case "minutes":
    document.getElementById('resultTime').innerHTML = 'Vc escolheu minutos';
    break;
case "hours":
    document.getElementById('resultTime').innerHTML = 'Vc escolheu Horas';
    break;

default:
    document.getElementById('resultTime').innerHTML = "no result";
    break;
  } 
}
<input type="text" id="convert" placeholder="Insira os segundos">
<br>
<button id="converter">Converter</button>
<select id="optionsTime">
<option id="minutes">Minutos</option>
<option id="hours">Horas</option>
<option id="teste">Teste</option>
</select>
<br>
<span id="resultTime"></span>
  

In the example above javascript should be placed after HTML.

  • example 2

function timesquare(s) {
var optionsTime = (s[s.selectedIndex].id);
var convert = document.getElementById('convert').value;
	
  if (convert < 60) {
	document.getElementById('resultTime').innerHTML = "Escolha um valor acima de 60";
	return;
  }
	
  switch(optionsTime){
	case "minutes":
		document.getElementById('resultTime').innerHTML = 'Vc escolheu minutos';
		break;
	case "hours":
		document.getElementById('resultTime').innerHTML = 'Vc escolheu Horas';
		break;
	
	default:
		document.getElementById('resultTime').innerHTML = "no result";
		break;
  } 
}
<input type="text" id="convert" placeholder="Insira os segundos">
<br>
<select id="optionsTime">
	<option id="minutes">Minutos</option>
	<option id="hours">Horas</option>
	<option id="teste">Teste</option>
</select>
<br>
<button onclick="timesquare(document.getElementById('optionsTime'));">Converter</button>
<span id="resultTime"></span>
  

The two examples above make use of the selectedIndex property - get or set the index that specifies the currently selected item, in this case the id .

Before giving another example, I'll tell you a bit more about this property SelectedIndex

When a user clicks on a pick in a pick list, the SelectedIndex quent property changes to a zero-based number corresponding to the position of that item in the list. The first item has a value of 0. This information is valuable for a script that needs to extract the value or text of a selected item for further processing.

You can also run the code with values in the options. See

  • example 3 with [s.selectedIndex].value)

    function timesquare(s) {
    var optionsTime = (s[s.selectedIndex].value);
    var convert = document.getElementById('convert').value;
    	
      if (convert < 60) {
    	document.getElementById('resultTime').innerHTML = "Escolha um valor acima de 60";
    	return;
      }
    	
      switch(optionsTime){
    	case "minutes":
    		document.getElementById('resultTime').innerHTML = 'Vc escolheu minutos';
    		break;
    	case "hours":
    		document.getElementById('resultTime').innerHTML = 'Vc escolheu Horas';
    		break;
    	
    	default:
    		document.getElementById('resultTime').innerHTML = "no result";
    		break;
      } 
    }
    <input type="text" id="convert" placeholder="Insira os segundos">
    <br>
    <select id="optionsTime">
    	<option value="minutes">Minutos</option>
    	<option value="hours">Horas</option>
    	<option value="teste">Teste</option>
    </select>
    <br>
    <button onclick="timesquare(document.getElementById('optionsTime'));">Converter</button>
    <span id="resultTime"></span>

And to finish, without id and without value in the options, by the text corresponding to the option

  • Example 4 with [s.selectedIndex].text)

    function timesquare(s) {
    var optionsTime = (s[s.selectedIndex].text);
    var convert = document.getElementById('convert').value;
    	
      if (convert < 60) {
    	document.getElementById('resultTime').innerHTML = "Escolha um valor acima de 60";
    	return;
      }
    	
      switch(optionsTime){
    	case "Minutes":
    		document.getElementById('resultTime').innerHTML = 'Vc escolheu minutos';
    		break;
    	case "Horas":
    		document.getElementById('resultTime').innerHTML = 'Vc escolheu Horas';
    		break;
    	
    	default:
    		document.getElementById('resultTime').innerHTML = "no result";
    		break;
      } 
    }
    <input type="text" id="convert" placeholder="Insira os segundos">
    <br>
    <select id="optionsTime">
    	<option>Minutos</option>
    	<option>Horas</option>
    	<option>Teste</option>
    </select>
    <br>
    <button onclick="timesquare(document.getElementById('optionsTime'));">Converter</button>
    <span id="resultTime"></span>

Conclusion

Your error was not due to using id or value in the options but rather how to make javascript retrieve this information.

  

I hope you have understood and I take this opportunity to ask you. What% of what would you do with the selected item?

    
05.08.2017 / 20:26