tanveerkhanwd's avatar

javascript to change on click input type

if 'tyoe = image' then show a upload input box otherwise url input box here is my view:

{!! Form::label('type', 'Type', ['class' => 'col-sm-2 control-label']); !!} {!! Form::select('type', ['1' => 'image', '2' => 'video'], null, ['placeholder' => 'Select Type','class'=>'form-control select2']); !!} {{ $errors->first('type') }}
0 likes
1 reply
mvd's avatar

Hi

You could do something like this (not tested).

  • Create a file input file with id 'file_input'
  • Create a text field with id 'text_input'

Hide both field if the form is loaded.

#text_input, #file_input { display: none; }

If select is changed.

<script type="text/javascript">
    document.getElementsByClassName('select2').addEventListener("change", function() {
      if (this.options[this.selectedIndex].value == 'image') {
        // Show file input.
        document.getElementById('file_input').style.display = 'block';

        // Hide and clear text input
        document.getElementById('text_input').value = '';
        document.getElementById('text_input').style.display = 'none';
      }
      else {
        // Hide and clear file field.
        document.getElementById('file_input').style.display = 'none';
        document.getElementById('file_input').value = '';

        // Show text input field
        document.getElementById('text_input').style.display = 'block';
      }
    });
  </script>

Other option is doing a ajax request and load the fields in the onchange function.

Also remember if you want to use the file input, you need to add 'files'=>'true' to your form.

Form::open(array('url' => '/form-submit-page', 'files'=>'true'));

Please or to participate in this conversation.