0

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.

2
  • Attribute selector: [data-bind]. Commented Jun 12, 2014 at 19:57
  • 1
    What exactly are you having troubles with? Selecting an element by attribute value? Commented Jun 12, 2014 at 20:07

2 Answers 2

2

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]');
}
Sign up to request clarification or add additional context in comments.

Comments

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

look at this fiddle

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.