How can I get the value from a Form::select in javascript / Laravel blades

Viewed 121

I'm trying to get the selected value from a Form::select in a blade's javascript file.

This is my blade:

{!!Form::open(['route' => [$complete == true ? 'completeAnalytical.store' : 'analytical.store'], 'method' => 'POST', 'id' => 'multiparametricAnalyticalForm'])!!}

{!! Form::select('fluid_type',['1' => 'Oil', '2' => 'Gas'], null, ['class'=>'form-control', 'id' => 'fluid_type']) !!}
            
{!! Form::submit('Run' , array('class' => 'btn btn-primary', 'id' => 'save')) !!}

This is the JS of my blade:

function myFunction() {
     var myVar = $("input[name = fluid_type]").val();
     console.log(myVar);
}

This is the console answer:

undefined

Is there any way to get the selected value ? Thanks so much for the help!

1 Answers

You may want to select the element by the id you've set for it.

Change the selector to

$("#fluid_type").val()

With your code

function myFunction() {
    var myVar = $("#fluid_type").val();
    console.log(myVar);
}

More information

Related