I want to get the value of $animal when I select an option so I can press the button and see detailed info of this option.
@foreach ($animais as $animal)
<option value="{{ $animal->id }}">{{ $animal->nome }}</option>
@endforeach
</select>
<a href="{{ route("animals.show", $animal) }}"><button type="submit">Ver</button></a>
CodePudding user response:
One the HTML page is written from the PHP, that value is a node in the DOM, and you can access it through javascript.
window.addEventListener('DOMContentLoaded', () => {
document.querySelector('select.example').addEventListener('change', e => {
console.log('value selected: ', e.target.value, 'from text', e.target.options[e.target.selectedIndex].text)
})
})
<select class='example'>
<option value='zebra'>animal</option>
<option value='carrot'>vegetable</option>
</select>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>