0

I am trying to get the selected value from the dropdown.

I am creating the controls dynamically. I am not using ID attribute to avoid the problem of having multiple controls with the same ID/ duplicate IDs.

Here is how I am able to get the values of the textbox controls

$('.btn-success').off('click').on('click', function (e) { 
    e.preventDefault();
    var row = $(this).closest(".row");
    var lnameval = row.find("input[name='ContactLastName']").val();        
});

Is it possible to get the selected value of the dropdown using the name attribute.

something like : var titleVal = row.find("input[name='ContactTitle']").val();

HTML :

 <form id="formAddContact"  role="form" class="form-horizontal">
            <div class="modal-body">
                <div id="errorMessageContainer2" class="alert alert-danger" role="alert" style="display:none;">
                    <ul id="messageBox2" class="list-unstyled"></ul>
                </div>
@foreach (string cInfo in Model.emailList)
        {

    <div class="row" id="@cInfo.Replace("@","")" style="display: none;">
        <div class="col-md-6">
            <div class="form-group">
                <div class="col-md-3 control-label">
                    <label>Title:</label>
                </div>
                <div class="col-md-3">
                    <select class="form-control ToCapture"  name="ContactTitle">
                        <option value="Mr">Mr</option>
                        <option value="Mrs">Mrs</option>
                        <option value="Ms">Ms</option>
                        <option value="Miss">Miss</option>
                        <option value="Dr">Dr</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3 control-label">
                    <label id="lblfname">First Name:</label>
                </div>
                <div class="col-md-3">
                    <input  maxlength="50" name="ContactFirstName" type="text" value="">
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-3 control-label">
                    <label id="lbllname">Last Name:</label>
                </div>
                <div class="col-md-3">
                    <input  maxlength="50" name="ContactLastName" type="text" value="">
                </div>
            </div>

        </div>

    </div>
    <div class="modal-footer">                 
        <input type="button" value="Add Contact"   class="btn btn-success">
        <input type="button" value="Cancel"  class="btn btn-default">    
    </div>
    <br/>
        }
                <hr />
            </div>           

    </form>

1 Answer 1

2

Just a little change needed :

row.find("select[name='ContactTitle']").val();

It's not an input.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.