I'm trying to split a string based on a particular character and then count the number of characters within each part. Is there a way to do this?
So, I have:
html
<a href="#" class="splitMe" title="Testing | this out">blah</a>
jquery
$(document).ready(function() {
$('.splitMe').each(function() {
var item = $(this).attr('title');
var characters = item.split("|");
// Here's where I get stuck...
// Tried various methods of length, but haven't been able to get it to work
// Most recent version that failed miserably...
var first = characters[0].text().length;
var second = characters[1].text().length;
alert(first+" "+second); //Yields characters[0] is not a function
});
});
I'm trying to split a string based on a particular character and then count the number of characters within each part. Is there a way to do this?
So, I have:
html
<a href="#" class="splitMe" title="Testing | this out">blah</a>
jquery
$(document).ready(function() {
$('.splitMe').each(function() {
var item = $(this).attr('title');
var characters = item.split("|");
// Here's where I get stuck...
// Tried various methods of length, but haven't been able to get it to work
// Most recent version that failed miserably...
var first = characters[0].text().length;
var second = characters[1].text().length;
alert(first+" "+second); //Yields characters[0] is not a function
});
});
Share
Improve this question
edited Oct 23, 2011 at 9:21
Eric
97.7k54 gold badges255 silver badges389 bronze badges
asked Oct 23, 2011 at 7:55
kevi kevikevi kevi
1651 gold badge6 silver badges12 bronze badges
1 Answer
Reset to default 10You have too much jQuery in your mind:
var first = characters[0].length;
var second = characters[1].length;
characters
is an array of strings, not jQuery objects. Strings don't have a .text()
method, they are already text. Just access their length
property.