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
});
});
You have too much jQuery in your mind:
charactersis an array of strings, not jQuery objects. Strings don’t have a.text()method, they are already text. Just access theirlengthproperty.