0

Excuse me, I'm just learning web developing recently,

I have some trouble on dynamic form, here's the picture

I wanna make dynamic form with select type on it.

TAMBAH button = add more field Simpan button = save,

The problem is, when I want to add more fields, select input did not show as the first one, it become normal input not select.

I know, maybe the problem is from javascript which I don't understand.

here is the code

On Form

<td>{{ Form::text('jumlah[]', null, array('class'=>'form-control')) }}</td>
<td>{{ Form::text('satuan[]', null, array('class'=>'form-control')) }}</td>
<td>{{ Form::text('keterangan[]', null, array('class'=>'form-control')) }}</td>
{{ Form::select('status_pi[]', ['Iya' => 'iya', 'Tidak' => 'Tidak'],
'Tidak',array('class'=>'form-control')) }}

On Javascript

var jumlah = document.createElement('input');
jumlah.setAttribute('name', 'jumlah[' + i + ']');
jumlah.setAttribute('class', 'form-control');

var satuan = document.createElement('input');
satuan.setAttribute('name', 'satuan[' + i + ']');
satuan.setAttribute('class', 'form-control');

var keterangan = document.createElement('input');
keterangan.setAttribute('name', 'keterangan[' + i + ']');
keterangan.setAttribute('class', 'form-control');

var status_pi = document.createElement('input');
status_pi.setAttribute('name', 'status_pi[' + i + ']');
status_pi.setAttribute('selected', 'selected');
status_pi.setAttribute('class', 'form-control');

I wanna make var status_pi field appear like on the first one below PI column,

Pls help sensei

janith1024
  • 1,042
  • 3
  • 12
  • 25
Isa9x
  • 3
  • 2
  • Your call to createElement should use `select` instead of `input` – James Jul 09 '18 at 03:45
  • It's work sir, the form become select field but it has no option value, what should i do to make the field has value like the first one? i don't know much javascript syntax, and not sure how to google what i mean – Isa9x Jul 09 '18 at 04:08
  • [Try this](https://stackoverflow.com/questions/17001961/how-to-add-drop-down-list-select-programmatically) – James Jul 09 '18 at 04:25
  • you didn't use option. you just use select. trying use select & option – Syed Nasrul Islam Anam Jul 09 '18 at 06:20

1 Answers1

0
var status_pi = document.createElement('input');

Should be:

var status_pi = document.createElement('select');

As you can see here the input field is for textfields, and a select field is for dropdown menus.

Keep in mind that you probably have to set your select's <option> values too in JavaScript.

Danoctum
  • 321
  • 5
  • 16