JQuery Get Selected Item from List Box and split by comma

47,745

Solution 1

Do you want to fetch the data on click or submit?

    $('select').click( function() {

        var selected = $(this).find(':selected').text();

        var substr = selected.split(',');
        var product = substr[0];
        var city = substr[1];
        var postcode = substr[2];
        var country = substr[3];

        //DO SOMETHING WITH IT??
    });

But its not a very good idea, considering your "data" structure can't change (ie. missing fields and so on) - since it will break on the indexes....

Solution 2

jquery has a selector for selected value.

so use

var selectedValue = $('#myselect option:selected').val();

then split on space

var table = selectedValue.split(',');

and loop on your table

for(i = 0 ; i < table.length; i++) {
    // dosomething with table[i]
}

Solution 3

suppose if you are use Html Listbox than try this

 <select id="Select1" size="4" multiple="multiple" onchange="Check(this);">
 <option value="1">item 1</option>
 <option value="2">item 2</option>
 <option value="3">item 3</option>
 <option value="4">item 4</option>
 <option value="0">All</option>
</select>

javascript

 function Check(selectObject)
  {
   var items = new Array();
   for (var i = 0, j = selectObject.options.length; i < j; i++)
   {
       if (selectObject.options[i].selected)
       {
           items.push(selectObject.options[i].value);
       }
   }
   // selectObject.id holds the "id"
   var itemsToString = items.toString();
   alert(itemsToString);
   //split and get values.
   var s = itemsToString.split(',');
   for(var k=0; k<s.length; k++)
      {
           alert(s[k]);
      }

  }

Hops its helps

Solution 4

Try $.map() to get all selected items in list

var selectedValues = $.map($('#ddlList option:selected'), function (element) {
        return element.value;
    }); 

Solution 5

Try this simple, call this function onchange() event of dropdown listbox .

function FuncFilter() {

     var selectedText = $("#DrpDatefilter").find(':selected').text();
     alert(selectedText );

    **Or** 
     var selectedValue = $("#DrpDatefilter").find(':selected').val();
     alert(selectedValue );

 }
Share:
47,745
Justin Erswell
Author by

Justin Erswell

By Day: I am the CTO for Travelfund.co.uk Ltd. By Night: I am an Apple geek, food lover, imbiber of good wine and husband.

Updated on April 06, 2020

Comments

  • Justin Erswell
    Justin Erswell about 4 years

    Can anyone offer more help on this I have the code setup on jsfiddle at...

    http://jsfiddle.net/justinerswell/feDTU/

    I need to get the last item in the substr and work backwards to populate the fields I need.

    Thanks

    Original Question

    Hi I have a list box returned on a page with multiple result which are being parsed in as a string into the value...

    <option value="1 Corn Exchange, The Strand, RYE, East Sussex, TN31 7DB, UNITED KINGDOM">
    

    Using JQuery I need to do a couple of things..

    1. Get Selected Value
    2. Split Value of Selected Item by Comma's
    3. Loop through in revers and set a variable for each item i.e country: United Kingdom Postcode:TN31 7DB etc

    Can anyone help me?

    Thanks