0

i have an select dropdown like that in my view:

<select name="table" class="form-control" id="db_table">
@foreach($tables as $table)
<option value="{{$table->id}}">{{$table->name}}</option>
@endforeach
</select>

And I want to pass the id which is in option value of the select to a link used in the same view. I can't use post form, because i want to use the value in another route.

MrGerwant
  • 17
  • 3
  • I'm not quite clear what you're asking for here. Are you looking to dynamically change the url of a link in the same page based on which option the user selects? – El_Vanja Nov 21 '20 at 15:48
  • Something like that. I want to get currently selected option value and pass it like that: – MrGerwant Nov 21 '20 at 15:53
  • It's important to know if you need to do it only on page load (once) or do you need it to be dynamic as the user changes it. – El_Vanja Nov 21 '20 at 15:55
  • I guess it has to be dynamically – MrGerwant Nov 21 '20 at 15:56
  • Does this answer your question? [Get selected value in dropdown list using JavaScript](https://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – El_Vanja Nov 21 '20 at 16:00
  • but can i get javascript variable using blade synthax? – MrGerwant Nov 21 '20 at 16:07
  • Blade is just a templating engine and has nothing to do with how javascript is written. Create your script and include it in your template as you would normally include it in an ordinary html page. – El_Vanja Nov 21 '20 at 16:10
  • ok so, to be clear, can I write var variable=document.getElementById('elementId').value and use it like {{$variable}} – MrGerwant Nov 21 '20 at 16:13
  • No, because js is client side. But when you're first loading your page, you don't need javascript to determine it, you know in your blade file which option will be selected by default. – El_Vanja Nov 21 '20 at 16:15
  • I'm lost. Assuming that i wrote a javascript function that sets the variable to be the value of select and set it to onchanged in select, what do I have to write inside to have that variable in the url? (edit: the url willbe more like "something/" – MrGerwant Nov 21 '20 at 17:30
  • Your default selected value. When you render a ` – El_Vanja Nov 21 '20 at 17:33
  • The function look like this: function getSelectedValue() { var v=document.getElementById('db_table').value; }. I don't think it changes the default selected option – MrGerwant Nov 21 '20 at 17:43
  • It doesn't change anything, it just fetches. Just use the id of the first option in blade `{{$tables[0]->id}}`. – El_Vanja Nov 21 '20 at 17:50
  • I tried it, now its always '1' in the url no matter which option i select – MrGerwant Nov 21 '20 at 18:03
  • Then most likely your javascript is not doing its job. Consult the linked question from my earlier comment. – El_Vanja Nov 21 '20 at 18:06
  • Do I have to write addEventListener inside script for this to work? – MrGerwant Nov 21 '20 at 19:34

1 Answers1

0

If i understand correctly you want to dynamically change the URL with the selected value from dropdown.

Use onchange event listener using JavaScript or JQuery which will allow you to get the selected value from select and then you can do whatever you want with that value.

Using JQuery

$("selecterIdOrClass").change(function(){
  $("selecterIdOrClass").attr('href',$(this).val());
});
Mudit Gulgulia
  • 1,131
  • 7
  • 21