harunB10
harunB10

Reputation: 5207

Remove 'disable' property from select box when another box is selected

I have two select boxes where user can choose category and subcategory. I want to subcategory be disabled by default and enabled when user has previously selected the category.

<select name="cat" onclick="this.form.submit()" style="width:220px;height:120px;" size="8">
    <option value="">All categories</option>
        @foreach($categoriesFilter as $cf)
            <option value="{{ $cf->mainCat_de }}">{{ $cf->mainCat_de }}</option>
        @endforeach
</select>

and

<select name="subcat" id="subcat" onchange="this.form.submit()" style="width:220px;height:120px;" size="8" disabled>
    <option value="">All subcategories</option>
        @foreach($subCategoriesFilter as $sf)
            <option value="{{ $sf->cat_de }}">{{ $sf->cat_de }}</option>
        @endforeach
</select>

I tried to do this, but it's not working:

<script type="text/javascript">
    var cat = {{ Request::get('cat') }};

    if( cat != "" || cat != null ){
        $('#subcat').prop('disabled', false);
    }
</script>

Upvotes: 0

Views: 118

Answers (1)

Toby Mellor
Toby Mellor

Reputation: 8205

If you wish to use Variable from Laravel's Blade that is a string, it must be wrapped in quotes.

What your code could equate to is var cat = fruits, which will throw a syntax error. By wrapping the blade in quotes, you're correctly saying var cat = "fruits".

var cat = "{{ Request::get('cat') }}";

$('#subcat').prop('disabled', cat == "" || cat == null);

Upvotes: 1

Related Questions