Suraj Shrestha
Suraj Shrestha

Reputation: 1808

passing values of dropdown to textbox

I have got a dropdownlist, a submit button and a textbox in my view. I want to pass the selected value of dropdownlist to the textbox when the submit button is clicked or onChange event of dropdownlist. How can I achieve this????

I solved it as follows:

<script type="text/javascript">
    $(function() {
        $('#ddlComp').change(function() {
            var selectedValue = $(this).val();
            $('#txtCompName').val(selectedValue);
        });
    });
</script>

<div>
    @Html.DropDownList("ddlcomp", Model.CompanyList)
    <input type="submit" value="Submit" />    
    @Html.TextBox("txtCompName")
</div>

Upvotes: 4

Views: 3850

Answers (1)

Sarfraz
Sarfraz

Reputation: 382696

I want to pass the selected value of dropdownlist to the textbox when the submit button is clicked or onChange event of dropdownlist

$(function() {
    var selectedValue = ''; // declare variable here

    // on drop down change
    $('#ddlComp').change(function() {
        selectedValue = $(this).val(); // store value in variable
        $('#txtCompName').val(selectedValue); // update on change
    });

    // on submit button click
    $('input[type=submit]').click(function(){
        $('#txtCompName').val(selectedValue); // update on submit button
    });
});

Upvotes: 1

Related Questions