R Saudlach
R Saudlach

Reputation: 388

Splitting Variable w/ JQuery

I thought I could split a string inside a jquery function using the JavaScript .split method. The code below stores string as "value1, value2, value3".

If I try to split that string and then alert the item, nothing happens. Any ideas? I have verified that string_sp is correct so I know the issue is with the split line.

$(document).ready(function(){
    $('.link_pressed').click(function() {
        var string_sp = $('select.s_box').val();
        var box_str = string_sp.split(',');
        alert(box_str[0]);
    });                 
});

Upvotes: 1

Views: 921

Answers (2)

ziesemer
ziesemer

Reputation: 28687

You may want to double-check your assumptions.

var string_sp = "value1, value2, value3";
string_sp.split(',');

Results in:

["value1", " value2", " value3"]

And:

var string_sp = "value1, value2, value3";
var box_str = string_sp.split(',');
box_str[0];

results in:

"value1"

Upvotes: 0

landons
landons

Reputation: 9547

You have a syntax error (didn't close the click function call). Try this:

$(document).ready(function() {
  $('.link_pressed').click(function() {
    var string_sp = $('select.s_box').val();
    var box_str = string_sp.split(',');
    alert(box_str[0]);
  });
}); 

Upvotes: 2

Related Questions