user441521
user441521

Reputation: 6998

Find data* attribute without specifying tag using jquery

Playing around with making a small data binding javascript library but I'm a little newer to javascript. Is there a way to just find the element, and all enclosing elements that have the data-bind attribute defined?

<form data-bind="Customer">
    <input type="text" id="name" data-bind="Name" data-bind-type="text" />
    <input type="text" id="birthday" data-bind="Birthday" data-bind-type="text" />
    <input type="text" id="address" data-bind="Address" data-bind-type="text" />
</form>

I want to define a function where I just pass in the "Customer" value and it will find the tag that has the data-bind = "Customer" (form in this case) and all tags within said containing tag that have the data-bind attribute defined. In this case it would return all 3 input tags so that I could examine them further.

Everything I've seen using jquery to do this is showing that I would need to know the "form" or the tag id to do this, but I'd prefer not to have to specify tag (like form) or id.

Upvotes: 0

Views: 164

Answers (2)

David Atchley
David Atchley

Reputation: 1204

You can at all data-bind elements within a particular data-bind element in this way:

$('[data-bind="Customer"] [data-bind]');

If you want to wrap that in a function, for instance if you need to access other wrapper elements with a different data-bind attribute value, you could do:

function getBoundElms(name) {
    return $('[data-bind="' + name + '"] [data-bind]');
}

Upvotes: 2

QBM5
QBM5

Reputation: 2788

$('[data-bind="Customer"]').children('[data-bind]')

look at this fiddle

http://jsfiddle.net/QBM5/M9eea/

Upvotes: 0

Related Questions