This appears to be a pretty common issue for a lot of people, but I haven't been able to figure out a solution that works.
I have a method that receives a DOM element from the caller. This DOM element happens to be a div, and I want to get an array of all of the buttons that are descendants of the div so that I can inspect them.
My first stop was to try var buttons = jQuery(element).find('button');
but this only returns the first button it finds.
However, if I do something like var buttons = jQuery('.someClass').find('button');
I get a list of all matched buttons.
Unfortunately, searching using a selector isn't an option, as I only have an element to work from.
One solution I found is at: jQuery find() returns only the first matched result ?. This person's question is almost identical to mine, but I can't get the solution to work, and also don't understand why jQuery isn't doing what I expect it to do here.
Other examples exist all over google and even SO, but most seem to target the use of selectors, not elements.
What don't I understand about jQuery that is preventing this from working the way I think it should, and how can I accomplish what I'm looking to?
Additional Information (is this maybe actually a knockout issue?)
It's a little difficult to give you a concrete set of HTML to look at because it's being rendered dynamically via knockout. That said, here is the a snippet that applies to this situation:
<!-- ko if: Answers.length == 4 -->
<div class="col-xs-12 col-xs-offset-0 col-sm-12 col-sm-offset-0" data-bind="ButtonTextReflowWidth: '120px'">
<!-- ko foreach: Answers -->
<div class="answer col-xs-12 col-sm-3" data-bind="css: { selectedAnswer: $parent.SelectedScaleValueID() == ScaleValueID }">
<button tabindex="-1" class="btn" data-bind="click: $parent.SetSelectedScaleValueID, text: Text, enable: ($parent.ParentVM.CurrentQuestion().QuestionID == $parent.QuestionID)" type="button"></button>
</div>
<!-- /ko -->
<!-- ko template: {name: 'editButtonTemplate'} --><!-- /ko -->
</div>
<!-- /ko -->
data-bind="ButtonReflowWidth: '120px'"
is the binding that pass its div as the element into a function that needs to find all of the buttons within the div.
Now that I look at it with fresher eyes, I'm wondering if this is really a knockout issue, rather than a jQuery issue. Could it be that the custom binding is being initialized before the DOM is aware that the template has been rendered out?
The anwsers
observable contains at least 4 items, which means that the button and its parent div.answer
should get rendered four times. I expect to find 4 buttons when making the jQuery find call.