1

I'm looking for a way to find out which field is selected with jQuery. I build a function, but it doesn't work.

HTML example

<select id="bewerbungID">
<option value="1" name="selectName">test1</option>
<option value="2" name="selectName">test2</option>
<option value="3" name="selectName">test3</option>
</select>

JS function

$('#bewerbungID select').on('change', function() {
   alert($('option[name=selectName]:checked', '#bewerbsungID').val()); 
});

Tested in jsfiddle open link

fcb1900
  • 238
  • 2
  • 18
  • Possible duplicate of [jquery select option click handler](http://stackoverflow.com/questions/5749597/jquery-select-option-click-handler) – Mohammad Apr 16 '17 at 10:26

3 Answers3

2

Treat select as an input field such as text. Then it has to have a value selected - show new value whenever it changes. You logic was right but jQuery selectors got a bit confusing.

Simplified your code a bit:

$('#bewerbungID').on('change', function() {
   alert($('#bewerbungID').val()); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="bewerbungID">
<option value="1" name="selectName">test1</option>
<option value="2" name="selectName">test2</option>
<option value="3" name="selectName">test3</option>
</select>
Marcin
  • 1,514
  • 1
  • 12
  • 27
1

The issue you are having is that you are not using the selector properly.

Basic Explanation:

The selector is everything you find within the parenthesis here: $('...'). When you use # in front of the word, that means you are selecting an element that has an id of whatever is to the right of #. In your case, ... is equal to #bewerbungID so the proper code should be as follows:

$('#bewerbungID').on('change', function() {
  alert($('#bewerbungID').val());
});
Webeng
  • 6,546
  • 4
  • 20
  • 51
0
$('#bewerbungID').on('change', function() {
   alert($('#bewerbsungID option:selected').val()); 
});

It should do the trick

Xurde
  • 1