Gerryblue
Gerryblue

Reputation: 5

Removing text from an output after a character

When processing a table with this function:

*$("tablelist div.panel",html).each(function( index ) {/*loop-start*/
    var job = {};/*init*/
    job.title = ($(this).text());
});/*loop-end*/*

I get as a result:

Text 1-Text2-Text3-Text4.

However, I just want Text 1 as a result. I don't want all the text after the first -.

I've tried many ways without success. Help is appreciated.

Upvotes: 0

Views: 31

Answers (3)

Tushar
Tushar

Reputation: 87203

You can use substring and indexOf:

var text = 'Text 1-Text2-Text3-Text4';

alert(text.substring(0, text.indexOf('-')));

substring will get the substring from 0th index to the indexOf index.

Upvotes: 2

Vrushali Pawar
Vrushali Pawar

Reputation: 3803

var str = "Text 1-Text2-Text3-Text4"
str.substr(0,str.indexOf("-"))

It will solve your problem.

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59232

You can split it on - and take the first element. String.split returns an array, whose first element at 0 index will give you the text you want.

(Remember indexes start from 0, so first is 0, second is 1 and so on).

job.title = $(this).text().split('-')[0];

Upvotes: 1

Related Questions