html - how to access multiple select array data in javascript -
i have multiple select box , want access selected data in javascript. here code:
<form onsubmit="return false;" id="multisel"> <select name="a[]" id="a" multiple style="width:350px;" tabindex="4"> <option value="pedro">1</option> <option value="alexis">2</option> <option value="messi">3</option> <option value="villa">4</option> <option value="andres">5</option> <option value="sergio">6</option> <option value="xavi">7</option> </select> <button id="btn1" onclick="ajaxmultiselect()" type="submit" class="btn btn-primary">save changes</button> <p id="status"></p> </form>
here code have tried far :
<script> function ajaxmultiselect(){ var input = []; input = document.getelementbyid("a").value; var status = _("status"); if(input == ""){ status.innerhtml = "fill out of form data"; }else { status.innerhtml = input; } } </script>
when run code gives first value. tried access values in php , works fine, passes value array in php. why isn't doing same javascript?
i tried run loop length of value calculates length of first selection only. want display values selected.
any appreciated.
you can following:
function getselectedoptions(element) { // validate element if(!element || !element.options) return []; //or null? // return html5 implementation of selectedoptions instead. if (element.selectedoptions) return element.selectedoptions; // here because browser doesn't have html5 selectedoptions var opts = element.options; var selectedoptions = []; for(var = 0; < opts.length; i++) { if(opts[i].selected) { selectedoptions.push(opts[i]); } } return selectedoptions; }
and change ajaxmultiselect()
call this:
input = getselectedoptions(document.getelementbyid("a"));
you have iterate values tho.
Comments
Post a Comment