0

I'm trying to find the selected value of a dropdown but I keep getting an empty string or undefined results.

My HTML looks like this:

<div class="input-group" style="padding-bottom: 10px;">
    <span id="add-addon-styling" class="input-group-addon">Status</span>
    <!-- TODO: Make this automatically rather than hardcoded -->
    <select class="form-control required" id="new-activity-modal-status-dropdown">
        <option value="NA" class="selected">N/A</option>
        <option value="ON_TRACK">On Track</option>
        <option value="ISSUE">Issue</option>
        <option value="BEHIND">Behind</option>
    </select>
    <span class="input-group-addon" data-toggle="tooltip" data-placement="top" title="The Status of the Activity. Usually On Track but otherwise set to N/A if unsure.">
        <b>?</b>
    </span>
</div>

And here is the JavaScript:

function OnModalCreateNewActivityBtnClick() {
    var jsonObject = '';
    var modal = $('new-activity-modal-body');
    var activityStatus = modal.find('#new-activity-modal-status-dropdown').val();
    ...
    ...
}

I've also seen this suggested:

function OnModalCreateNewActivityBtnClick() {
    var jsonObject = '';
    var modal = $('new-activity-modal-body');
    var activityStatus = modal.find('#new-activity-modal-status-dropdown').find(":selected").text();
    ...
    ...
}

The first returns an undefined result. The second returns an empty string.

I'm not sure what to do. JavaScript still perplexes me.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
OmniOwl
  • 5,477
  • 17
  • 67
  • 116

4 Answers4

2
var modal = $('new-activity-modal-body');

Should probably be

var modal = $('#new-activity-modal-body');

You get no errors, since jquery selectors finding nothing is a valid situtation.

ekuusela
  • 5,034
  • 1
  • 25
  • 43
1

Assuming the HTML is :

<div class="new-activity-modal-body">...</div>

The following code will not work because you don't specify if new-activity-modal-body is a class or id :

var modal = $('new-activity-modal-body');

Should be :

var modal = $('#new-activity-modal-body'); //id selector
//OR
var modal = $('.new-activity-modal-body'); //class selector

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

Use

var modal = $('#new-activity-modal-body');
lradacher
  • 470
  • 4
  • 6
1

Your variable modal is not assigned properly. There is no new-activity-modal-body. You should use #new-activity-modal-body.

Just replace

var modal = $('new-activity-modal-body');

With

var modal = $('#new-activity-modal-body');
Ali Zia
  • 3,825
  • 5
  • 29
  • 77