Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split items separated by commas (,) in Javascript

My Javascript var contains a 2D array. If I pop an alert on the the var i get the JSON serialized result, something like:

ID0, DESCRIPTION

I'd like to get each items separated by the , in the value option of the dropdownlist and the other item in the description.

Here's my Javascript code, it would work if split was working correctly but this pops an error because the var doesn't contain a pure string type.

$.ajax(
        {
            type: "POST",
            url: "Projet.aspx/GetDir",
            data: "{VP:'" + dd_effort_vp + "',DP:'" + dd_effort_dp + "',Direction:'" + dd_effort_d + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
                   var cars = response.d;
                    $.each(cars, function(index, value) {
                    $('#<%= dd_effort_directionp.clientid()%>').append(
                 $('<option</option>').val(value[value.split(",",0)]).html(value.split(",",1))





                }
            }
        });

I know split doesn't work that way here because of the return value is not a string but you get the result i'd like to achieve, get the first value before the comma has the VALUE of the Dropdownlist and the item after the comma as the HTML text.

Thanks ALOT!

like image 629
Tommy Dubé-Leblanc Avatar asked Jan 27 '26 01:01

Tommy Dubé-Leblanc


2 Answers

How about value.split(",")[0] instead of value.split(",",0)?

like image 53
codeling Avatar answered Jan 28 '26 13:01

codeling


Have you tried value.toString().split(",")?

like image 41
IronicMuffin Avatar answered Jan 28 '26 15:01

IronicMuffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!