azamsharp
azamsharp

Reputation: 20066

jQuery Find Element Using Property

I am using jquery to locate some elements from a table. The problem is that I might not know all the time the type of the element. That means an element can be a simple TD or a SELECT. But since both have text associated with it I want to retrieve the element if that has the text property.

$($(rows[i]).find("element that has property text")[0]).text();

UPDATE 1: I updated the code and the td text node has a value but the currentRow.triggerType is returned empty.

currentRow.triggerType = $($(rows[i]).find('td')[0]).filter(function() {
            return this.nodeType == 3; 
        }).text();

Upvotes: 1

Views: 743

Answers (4)

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

You can apply a custom filter function - in the following example, we reduce the result set to items that contain a text node (nodeType == 3)

$('td').filter(function() {
    return this.nodeType == 3;
}).text()

Update

Your selector should be

$(rows[i]).find('td').first().filter(...

Upvotes: 2

Stefan
Stefan

Reputation: 5672

It´s not clear what you´d like to do with the values but here are some ways to get them;

$('td:first, select').each(function() {
    var text = $(this).val() || $(this).text();
    console.log(text);
});

If some td elements contains select elements;

$('td').each(function() {
    var $this = $(this),
        $select = $this.find('select:first'),
        text

    text = ($select.length == 0) ? $this.text() : $select.val();
    console.log(text);
});

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20620

Are you looking for the contains selector?

http://jsfiddle.net/W9EMM/

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227240

I don't really understand the question, but would this work?

$(rows[i]).filter(function(){
  return $(this).text() !== '';
}).text();

You can also try this:

$(rows[i]).not(':empty').text();

Upvotes: 0

Related Questions