A page deals with an input field that changes behavior depending on the dropdown value.
<div class="form-group">
<label for="exampleInputEmail2">Element Name</label>
<select class="form-control" name="element_id" id="element_name">
@foreach($elements as $element)
<option value="{{ $element->element_id }}">{{ $element->element_name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleInputName2">Element Value</label>
<input type="text" class="form-control" id="element_value" name="value" placeholder="Add an Element Value">
</div>
A script I have is below:
<script type="text/javascript">
<?php
echo "var javascript_array = ". $list . ";\n";
?>
$("#element_name").on('change', function(){
var id_input = $('#element_name').val();
if(id_input == 2){
$("#element_value").datepicker();
}
else if(jQuery.inArray(id_input, javascript_array)!='-1'){
$("#element_value").datepicker('destroy');
$( function() {
$("#element_value").autocomplete({
scroll: true,
autoFocus: true,
minLength: 3,
source: function( request, response ) {
$.ajax({
url: "/employees/public/admin_list/search",
dataType: "json",
data: {
searchText: request.term
},
success: function( data ) {
response($.map(data, function(item) {
return {
label: item.name,
value: item.id
};
}));
}
});
},
select: function( event, ui) {
$(this).val(ui.item.label);
return false;
}
} );
} );
}
else {
$('#element_value').datepicker('destroy');
}
});
</script>
So basically, if the value of the dropdown is 2(Birthday), the field changes to datepicker (which works), unfortunately, the part where else if(jQuery.inArray(id_input, javascript_array)!='-1') doesn't seem to work, as it doesn't change the input to a search input. (If I hardcode it to else if((id_input == 1) || (id_input == 4) || (id_input == 5)) it always works, but this list ids will increase as users creates them in the future, so I have to store them in javascript_array and just search from there).
What am I missing?
the array shows as
var javascript_array = [1,4,5];
generated from a controller as below:
$lists = Elements::select('element_id')->where('is_list', 1)->get();
foreach ($lists as $r){
$list_temp[] = $r->element_id;
}
$list = json_encode($list_temp);
javascript_arrayis?javascript_array)!='-1'tojavascript_array)!=-1