André Martins André Martins - 1 year ago 35
jQuery Question

.text() not returning value

I'm using this code to create a

of the classes attached to the div


var alts = {};
var classes2 = $(this).attr('class').split(' ');
for (var i = 0; i < classes2.length; i++) {
var matches2 = /^tipo\-(.+)/.exec(classes2[i]);
if (matches2 != null) {
var produto = matches2[1];
alts[classes2] = true;
$('ul.filters').append('<li class="filter-produto">'+ produto +'</li>');

The code above generates
s inside of the
<ul class="filters">
. So the code is something like this:

<ul class="filters">
<li class="filter-produto active">Produto 1<li>
<li class="filter-produto">Produto 2<li>
<li class="filter-produto">Produto 3<li>
<li class="filter-produto">Produto 4<li>

As you can see, one of the
s will have an additional class named "active", and the problem is I need to get the text inside of it (Produto 1) to use on another code but it's not working:

var produto2 = $('').text();

I tested
to see if the code worked but it only returns a blank value and not the text inside the

dee dee
Answer Source

Sadly I don't have rep to comment, but can you show us the order of the stuff happening?

Perhaps you are asking for the value before the active class it attached to the li. Something like this:

// Function to create the ul and lis

var produto2 = $('').text();

// Function that adds the active class

So maybe the doesn't exist when you ask for it's value?