-4

I am pretty new in JavaScript and jQuery and I have the following problem trying to retrieve the value related to the selected option of a select.

So, into my page I have:

<select id="selAttivitaSelezionata" class="form-control valid" name="selAttivita" aria-invalid="false">
    <option value="valida">Valida Progetto</option>
    <option value="invalida">Rimuovi Validazione</option>
</select>

then I have the following jQuery code that is performed every time that the user select an option inside the previous select:

$("#selAttivitaSelezionata").change(function() {
    //alert("TIPOLOGIA PROGETTO CAMBIATA");
                    
    var sel = $("#selAttivitaSelezionata");
    var val = sel.value;
    alert(val);
                    
    $("#statoProgettoLabel").hide();
    $("#selStatoProgetto").hide();
});

So, as you can see first I select the select object in the DOM having id="selAttivitaSelezionata" and then I try to obtain the selected value by sel.value, then I print it into an alert popup

The problem is that the alert is empty. Using the FireBug debugger I see that the sel variable is correctly initialized but the val variable is undefined as value obtained by sel.value.

Why? What am I missing? How can I obtain the value of the value attribute (valida or invalida)?

halfer
  • 19,824
  • 17
  • 99
  • 186
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • The node element ` – PeterKA Nov 03 '15 at 14:32

5 Answers5

5

Because you have this:

var sel = $('#selAttivitaSelezionata');

Which makes sel a jQuery object.

This:

var val = sel.value;

Should be this:

var val = sel.val();

As .val() is the jQuery method for retrieving the value.

You could make this much better by using $(this):

$("#selAttivitaSelezionata").change(function() {
    var val = $(this).val();
    $("#statoProgettoLabel, #selStatoProgetto").hide();
});
TheCarver
  • 19,391
  • 25
  • 99
  • 149
  • 1
    The best would be in this case of a single select element: `this.value` – A. Wolff Nov 03 '15 at 14:35
  • 1
    Only [this answer](http://stackoverflow.com/a/33501540/2025923) explain why `sell.value` doesn't work. :) – Tushar Nov 03 '15 at 14:39
  • @Tushar: Yes, but yours is the only answer that will confuse people with references to VanillaJS. Not that many people, especially beginners like the OP, know that VanillaJS is really just plain old Javascript. VanillaJS was created as a joke and I don't think it should be used to reference Javascript. What's wrong with saying 'Javascript'? http://stackoverflow.com/questions/20435653/what-is-vanillajs – TheCarver Nov 03 '15 at 15:06
3

Here is what you're looking for:

var val = sel.val();

Here is a reference to jQuery documentation http://api.jquery.com/val/

Stepashka
  • 2,648
  • 20
  • 23
3

sel is jQuery object, you cannot use Javascript methods on it.

Use val() on it to get the value.

sel.val();

If you want to use Javascript methods,

var sel = $("#selAttivitaSelezionata")[0];
var val = sel.value;
Tushar
  • 85,780
  • 21
  • 159
  • 179
3

You don't need two elements, instead just use the one along with the .val() jQuery function.

Working example

$("#selAttivitaSelezionata").change(function() {
  var val = $(this).val();
  console.log(val);

  $("#statoProgettoLabel").hide();
  $("#selStatoProgetto").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="selAttivitaSelezionata" class="form-control valid" name="selAttivita" aria-invalid="false">
  <option value="valida">Valida Progetto</option>
  <option value="invalida">Rimuovi Validazione</option>
</select>
Stewartside
  • 20,378
  • 12
  • 60
  • 81
2

Don't query the DOM again inside your "change" handler; jQuery gives you a reference to the element (the value of this):

$("#selAttivitaSelezionata").change(function() {
  var sel = $(this);
  var val = sel.val();

  // ...
});
Henry Situ
  • 153
  • 14
Pointy
  • 405,095
  • 59
  • 585
  • 614