0

I am storing multiple values in a select option and trying to get the all the values separately by splitting the value. Split seems to work as I can get the first [0] value, but I am not getting any value after that. It alerts back as undefined. Code below. Thanks.

function roi_calc(e) {
var merchant_option = $(e).prev('td').prev('td').prev('td').find('select').val();
if (merchant_option!='Merchant') {
    var value_input = $(e).prev('td').prev('td').find('input').val();
    if ((value_input!='') && (value_input!='Value')) {
        var price_input = $(e).prev('td').find('input').val();
         if ((price_input!='') && (price_input!='Price')) {
            alert ('Merchant value = '+merchant_option);
            //var price_arr = new Array();//tried setting array first, but same results
            var price_arr = price_input.split(',',3);
            //alert (price_arr);//alerts '1'
            alert (price_arr[0]);//working - alerts '1'
            alert (price_arr[1]);//not working - undefined
            alert (price_arr[2]);//not working - undefined
            }
            else {alert ('Price must be entered.');}
        }
        else {alert ('Value must be entered.');}
    }
    else {alert ('Merchant must be selected');}
//alert (row_option);

}

HTML

<table style="width: 600px;">
<tr>
<td>
    <select>
        <option>Merchant</option>
        <option value="1,2,3,4">1</option>
        <option value="5,6,7,8">2</option>
    </select>
</td>
<td><input value="Value"></td>
<td><input value="Price"></td>
<td onclick="roi_calc(this);"> R.O.I </td>
</tr>
</table>

on jsfiddle http://jsfiddle.net/upywN/

FOUND THE ANSWER. Stupid mistake on my part.
I was splitting price_input, but needed to be splitting merchant_option stupid mistake sorry to waste everyone's time.

TDave00
  • 274
  • 6
  • 18

2 Answers2

1

this is typically invalid HTML

<option value="1,2,3,4">1</option>

see the property of option

instead you should use multiple select like this

<select multiple="multiple">

xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • alert ('Merchant value = '+merchant_option); alerts out 1,2,3,4 so, I am getting the value I want. – TDave00 Mar 05 '12 at 08:24
  • nope. that just selects multiple options. I need to get the values from one option and split them so instead of the string '1,2,3,4' I have an array of '1,2,3,4' – TDave00 Mar 05 '12 at 08:28
  • I mean an array of '1','2','3','4' – TDave00 Mar 05 '12 at 09:03
0

Update: You are capturing the 'selected value' (ie. '1' not '1,2,3,4') form

<option value="1,2,3,4">1</option>

But you have to split 'text' part (i.e. '1,2,3,4').

You can use Javascript for grabbing the 'text' part of option like this:

var list = document.getElementById('list');  // 'list' is the id of select element
var text = list.options[list.selectedIndex].text;
//And now use your split
var price_arr = text.split(',',3);
tusar
  • 3,364
  • 6
  • 37
  • 60