Reputation: 5
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
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 0
th index to the indexOf
index.
Upvotes: 2
Reputation: 3803
var str = "Text 1-Text2-Text3-Text4"
str.substr(0,str.indexOf("-"))
It will solve your problem.
Upvotes: 0
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