javascript - Button to add selected="selected" from outside form -
i using laravel, , trying integrate jquery, not at.
i have multiple select box potentially lots of options, based on database values.
<div class="toolselect"> <select multiple> <option value="1">tool 1</option> <option value="2">tool 2</option> <option value="3">tool 3</option> </select> </div>
the user can select tools directly select box, can huge list , therefore displaying search field below select box search database of tools user can see more info tool, , decide if wants include it.
currently user has first search, find entry in select box , select it.
i want faster solution button next info tool, automatically adds selected="selected" correct option in select box above.
i tried
<input type="button" value="mer" onclick="addselect('{{$tool->id}}')"/> <script type="text/javascript"> addselect = function (id){ $("div.toolselect select").val(id); }
but erased other other selected fields.
any ideas appreciated.
for multi-select, value array of selected items. you're setting value single item, treated array of 1 element, other selections discarded.
so should old value, add new id it, , set new value.
function addselect(id) { $('.toolselect select').val(function(i, oldval) { oldval = oldval || []; // oldval = null when nothing selected oldval.push(id); return oldval; }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="toolselect"> <select multiple> <option value="1">tool 1</option> <option value="2">tool 2</option> <option value="3">tool 3</option> </select> </div> <input type="button" value="select 1" onclick="addselect('1')"/> <input type="button" value="select 2" onclick="addselect('2')"/> <input type="button" value="select 3" onclick="addselect('3')"/>
Comments
Post a Comment