javascript - How to post all options in a select element with stringify the text and values? -


there select element have options created dynamically. want post these options text-value pairs via ajax etc...

is possible make them stringify text-value pairs? or suggestions without stringifying text-value pairs. thanks.

use if don't have jquery

function getselectoptions(id){     var select = document.getelementbyid('x');     var obj = {};     for(var i=0; i< select.options.length; i++){         var option = select.options[i];         obj[option.value] = option.innerhtml;     }     return obj;     } var opts = getselectoptions('x'); console.log(opts, json.stringify(opts)) 

demo: fiddle

with jquery

function getselectoptions(id){     var select = $('#' + id);     var obj = {};     $('option', select).each(function(i, v){         var $this = $(this);         obj[$this.val()] = $this.text();     });     return obj;     } var opts = getselectoptions('x'); console.log(opts, json.stringify(opts)) 

demo: fiddle


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -