CodeIgniter Forums
Clearing a select box after form submit. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Clearing a select box after form submit. (/showthread.php?tid=1111)



Clearing a select box after form submit. - BrandenM - 02-12-2015

Code:
<select class="form-control" id="project_type" name="project_type">
<option value="">Select..</option>
<?php foreach ($project_types as $value): ?>
<option value="<?= $value['PROJTYP_CODE'] ?>" <?= set_select('project_type', $value['PROJTYP_CODE']); ?>><?= $value['PROJTYP_CODE'].' - '.$value['PROJTYP_DSC'] ?></option>
<?php endforeach ?>
</select>

I have a select box that I am dynamically creating options for with php and repopulating the input with CI's set_select().

Everything works as expected but when I try to clear the form, the repopulated option acts as the selectedIndex ( as it should ) and I can't reset the input to the top option. I've tried a couple solutions with jQuery but had no luck.

Has any one else ran into this issue? if so how did you handle it?


RE: Clearing a select box after form submit. - mwhitney - 02-13-2015

In jQuery, you would just use $('#project_type').prop('selectedIndex', 0);


RE: Clearing a select box after form submit. - RobertSF - 02-13-2015

(02-12-2015, 06:46 AM)BrandenM Wrote: when I try to clear the form, the repopulated option acts as the selectedIndex ( as it should ) and I can't reset the input to the top option.
I think I had a problem similar to that, and I solved it this way.
PHP Code:
   <select name="gift_list]">
 
     <option value="0" <?= set_select('gift_list''0'?>>Select...</option> 
Basically, I treated the top option as a valid selection with value zero. If the user doesn't make a different selection (one that has a non-zero value), I can catch that during validation.