Help me please
Sep 1, 2022
18
Level 1
Issue with Select Option.
I have a Select menu where have 4 Options. 1.Text 2.Textare 3.Checkbox 4.Radio Everytime will chose an option will get the an input with the appropriate selection. Issue will happen when chose Checkbox or Radio, will want to show another input with buttons that allow user to add more options, but on select all this will be show and instantly removed. Please help me fixing the code to work.
Blade
<h2 class="text-white font-bold flex items-center justify-center">Add Question</h2>
<div class="bg-white rounded shadow p-2">
<form wire:submit.prevent="editsurveyqdata">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="input-field col s12">
<select wire:model="question_type" id="question_type" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded focus:outline-none block w-full p-2.5">
<option value="" disabled selected>Choose your option</option>
<option value="text">Text</option>
<option value="textarea">Textarea</option>
<option value="checkbox">Checkbox</option>
<option value="radio">Radio Buttons</option>
</select>
</div>
<div class="p-1 mb-2">
<label for="title" class="text-gray-700 font-semibold">Question</label>
<input id="title" type="text" wire:model="qtitle" class="bg-gray-50 border border-gray-300 text-gray-700 text-sm rounded focus:outline-none block w-full p-1" required>
</div>
<!-- this part will be chewed by script in init.js -->
<span class="form-g"></span>
<div class="input-field col s12">
<button type="submit" class="px-2 py-1 rounded bg-green-700 text-white">Submit</button>
</div>
</div>
</form>
</div>
Java Script
<script>
$(document).ready(function() {
// will replace .form-g class when referenced
var material = '<div class="input-field input-g p-1 flex space-x-2">' +
'<input id="option_name[]" wire:model.defer="qoption_name[]" type="text" class="bg-gray-50 border border-gray-300 text-gray-700 text-sm rounded focus:outline-none block p-1">' +
'<span class="add-option bg-green-700 rounded px-2 py-1 text-white cursor-pointer">Add Another</span>' +
'<span class="delete-option bg-red-700 rounded px-2 py-1 text-white cursor-pointer">Delete</span>' +
'</div>';
$(document).on('click', '.delete-option', function() {
$(this).parent(".input-field").remove();
});
// for adding new option
$(document).on('click', '.add-option', function() {
$(".form-g").append(material);
});
// allow for more options if radio or checkbox is enabled
$(document).on('change', '#question_type', function() {
var selected_option = $('#question_type :selected').val();
if (selected_option === "radio" || selected_option === "checkbox") {
$(".form-g").html(material);
} else {
$(".input-g").remove();
}
});
});
</script>
Please or to participate in this conversation.