AM DEV
AM DEV

Reputation: 339

How to make an option selected according to the value of an input given

In my DB table sources i have some sources with their url's. im getting their values using eloquent:

<?php $src = App\Source::orderBy("name", "ASC")->get()?>
<select class="form-control selectedSRC" name="sources">
  @foreach($src as $option)
    <option value="{{$option->id}}">{{$option->name}}</option>
  @endforeach
</select>

I have an input for a url:

<input class="sources col-md-3 srcURL" 
type="url" class="form-control"
name="sourcesUrl" placeholder="Source URL">

When I enter an url in the input, I want the name of the source to be selected by checking if the url of the source is contained within the url of the input. Ex:

input url: www.google.com/images/smiley.jpg
src in DB: www.google.com (with name: GOOGLE)

When I input the url above i want GOOGLE option to be selected

Upvotes: 0

Views: 61

Answers (1)

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

To do like that as you say above

First you have to change the select option Like

   <?php $src = App\Source::orderBy("name", "ASC")->get()?>
    <select class="form-control selectedSRC" name="sources">
        @foreach($src as $option)
            <option value="{{$option->url}}">{{$option->name}}</option>
        @endforeach
    </select>

Now Just add this Jquery to your page then it will work perfect.

$('.sources .col-md-3 .srcURL').on('change',function(){
        $(".form-control .selectedSRC").val($(this).val());    
    });

Upvotes: 1

Related Questions