Get a value of a fieldset with javascript

0

I am trying to get the values that a user chooses from fieldset to then show a alert that shows the price depending on the chosen option.

function formAlertCompra(){
var year = fieldset.getElementById('año').value; 
var result_year=year*10;		 
				 alert(result_year);
				 return false;
}	
<form ethod="post" onsubmit="formAlertCompra();"><fieldset data-role="fieldcontain" id="año"> 
				<label for="año">Año:</label>
				<select id="año_compra" name="año_compra">
					<option>Seleccione Uno</option>
					<option>2015</option>
					<option>2016</option>
					<option>2017</option>
					<option>2018</option>
				</select>
	<input type="submit" value="Register">			</fieldset>
</form>

However, when selecting the year, no alert is coming out. What would be my mistake?

    
asked by Stephanie B Bautista 10.07.2018 в 04:10
source

1 answer

1

Before we start, we go for some general observations:

  • The method is written as method , you needed an "m".
  • In each onsubmit , to avoid the behavior of sending the request there are several ways, one of them being the "return" of a variable as false.
  • The Fieldsets lack a defined value, according to the context, what you are looking for is the value that is within the options shown, therefore the required value is select .
  • The following code solves your problem.

    function formAlertCompra(){
    var year = fieldset.getElementById("año_compra").value; 
    var result_year=year*10;		 
    alert(result_year);
    return false;
    }	
    <form method="post" onsubmit="return formAlertCompra();">
        <fieldset data-role="fieldcontain" id="año"> 
           <label for="año">Año:</label>
    	<select id="año_compra" name="año_compra">
    	    <option>Seleccione Uno</option>
    		<option>2015</option>
    		<option>2016</option>
    		<option>2017</option>
    		<option>2018</option>
    	</select>
    	<input type="submit" value="Register">	
        </fieldset>
    </form>
        
    answered by 10.07.2018 / 05:43
    source