Demsaas Demsaas - 1 year ago 52
Javascript Question

Select option of dropdown and make it not active?

I got select :

<select class="form-control" id="category_id_select" name="Category_Id">
@foreach (var category in Model.categories){
<option value="@category.Id">@category.Name</option>

And I got the input where I set value that must be selected on page load :

<input type="text" class="form-control" id="cat_id" value="@Model.CategoryId"/>

My js file is :

$(document).load(function () {
var catid = $("#cat_id").val();
$("#category_id_select ").val(catid);
$("#add_prop").click(function () {

What am I doing wrong?

I will hide that input and wanna to have not active dropdown with already selected value.Whats solution with jquery?

Answer Source

You need to trigger the value change that you made :


NOTE : Remove the extra space you've in your selector :

$("#category_id_select ").

And try to use ready function :

     //Your code here

Hope this helps.

$(function () {
    var catid = $("#cat_id").val();
<script src=""></script>
<select class="form-control" id="category_id_select" name="Category_Id">
    <option value="c1">category 1</option>
    <option value="c2">category 2</option>
    <option value="c3">category 3</option>

<input type="text" class="form-control" id="cat_id" value="c3"/>