PHP – Find parent key of array

A little crude recursion, but it should work: function find_parent($array, $needle, $parent = null) { foreach ($array as $key => $value) { if (is_array($value)) { $pass = $parent; if (is_string($key)) { $pass = $key; } $found = find_parent($value, $needle, $pass); if ($found !== false) { return $found; } } else if ($key === ‘id’ && … Read more

Iterating over element attributes with jQuery

The best way is to use the node object directly by using its attributes property. The only difference in my solution below compared to others suggesting this method is that i would use .each again instead of a traditional js loop: $(xml).find(‘item’).each(function() { $.each(this.attributes, function(i, attrib){ var name = attrib.name; var value = attrib.value; // … Read more

How to get nodes lying inside a range with javascript?

Here’s an implementation I came up with to solve this: function getNextNode(node) { if (node.firstChild) return node.firstChild; while (node) { if (node.nextSibling) return node.nextSibling; node = node.parentNode; } } function getNodesInRange(range) { var start = range.startContainer; var end = range.endContainer; var commonAncestor = range.commonAncestorContainer; var nodes = []; var node; // walk parent nodes from … Read more

How can I find the closest previous sibling with class using jQuery?

Try: $(‘li.current_sub’).prevAll(“li.par_cat:first”); Tested it with your markup: $(‘li.current_sub’).prevAll(“li.par_cat:first”).text(“woohoo”); <script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script> <ul> <li class=”par_cat”></li> <li class=”sub_cat”></li> <li class=”sub_cat”></li> <li class=”par_cat”>// this is the single element I need to select</li> <li class=”sub_cat”></li> <li class=”sub_cat”></li> <li class=”sub_cat current_sub”>// this is where I need to start searching</li> <li class=”par_cat”></li> <li class=”sub_cat”></li> <li class=”par_cat”></li> </ul> will fill up the closest … Read more

How to select all content between two tags in jQuery

Two methods in particular would be very useful solving this problem: .nextUntil, and .andSelf. The first will grab all of the siblings following your selector, and the latter will lump in whatever is matched by your selector as well, giving you one jQuery object that includes them all: $(“#heading2”) .nextUntil(“#heading3”).andSelf() .css(“background”, “red”); This results in … Read more