explode string in jquery

45

I'm getting the following result through ajax.

row=Shimla|1|http://vinspro.org/travel/ind/

I wanna http://vinspro.org/travel/ind/ from it. I have used find and split function but it is not working . please let me know how I can get it?

var result=$(row).split('|');
    alert(result);

chrome showing the following error

Uncaught Error: Syntax error, unrecognized expression: Shimla|1|http://vinspro.org/travel/ind/ 

This question is tagged with javascript jquery

~ Asked on 2012-12-24 19:33:17

The Best Answer is


122

The split method will create an array. So you need to access the third element in your case..

(arrays are 0-indexed) You need to access result[2] to get the url

var result = $(row).text().split('|');
alert( result[2] );

You do not give us enough information to know what row is, exactly.. So depending on how you acquire the variable row you might need to do one of the following.

  • if row is a string then row.split('|');
  • if it is a DOM element then $(row).text().split('|');
  • if it is an input element then $(row).val().split('|');

~ Answered on 2012-12-24 19:35:47


7

Split creates an array . You can access the individual values by using a index.

var result=$(row).val().split('|')[2]
alert(result);

OR

var result=$(row).val().split('|');
alert(result[2]);

If it's input element then you need to use $(row).val() to get the value..

Otherwise you would need to use $(row).text() or $(row).html()

~ Answered on 2012-12-24 19:34:26


Most Viewed Questions: