Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Iljido's avatar

how to display current selected option when using onchange() function on select tag

hello, in my dropdown menu, i linked the option tag to a url , it goes to the url, but the the dropdown list is showing "select channel" instead of the option selected

here's the code :

<select class="form-control" id="channel_list" name="channel_list"  onchange="location = this.value;">
                            <option value="">Select Channel</option>
                            @foreach(App\Channel::all() as $channel)
                                <option value="/threads/{{ $channel->slug }}" >
                                        {{ $channel->name }}
                                </option>
                            @endforeach
 </select>
0 likes
5 replies
Iljido's avatar

@jlrdw it seems when i go to a specific link it refresh so the selected option is no longer selected, instead i get "select channel" , :/ (it shows the result but would be nice if even if i go to another page the selected option keep the same )

jlrdw's avatar

Are you using Ajax to reload just part of the page if not that's probably what you need to do.

Snapey's avatar
Snapey
Best Answer
Level 122

of course if goes back to select channel. You have done nothing to tell it which entry it should be on.

perhaps something lke

<select class="form-control" id="channel_list" name="channel_list"  onchange="location = this.value;">
    <option value="">Select Channel</option>
    @foreach(App\Channel::all() as $channel)
        <option value="/threads/{{ $channel->slug }}"
        @if (request()->is('threads/' . $channel->slug)) selected @endif
        >
        {{ $channel->name }}
        </option>
    @endforeach
 </select>
1 like

Please or to participate in this conversation.