4

I have the following code, which is written in DOM API instead of jquery. I'm not sure why. It's inside the submit function for jquery form validation. I need to change the "parentNode" part so that instead of "parent" it is "closest." I'm no good with javascript. I tried my hand at converting this to jquery, but wasn't able to get that to work. Basically, if this were jquery, I'd need to change .parent() to .closest().

var summary = "";

$.each(element, function() { 
summary += "<li>" + this.element.parentNode.getElementsByTagName('label')[0].innerHTML.replace("\<span\>*\<\/span\>","") + "</li>"; 
});

summary = summary.replace(/\<li\>.+\<\/li\>\1/, "$1");

alert(summary);

Is this possible to do with javascript? Or, is there an easy way to convert this to jquery?

UPDATE: Here is a fiddle to help explain what I'm trying to accomplish. Basically, because I added a 'span' tag around one of the inputs, the 'p' tag is no longer the parent. As such, the parentNode is not finding the 'p' tag anymore.

http://jsfiddle.net/9um8xt79/1/

UPDATE 2 -- THE previous fiddle link was incorrect. The link above is the right one.

How can I modify to find the <p> tag regardless of whether it is the direct parent, or a grandparent?

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
Lindsay
  • 127
  • 1
  • 3
  • 11
  • 6
    "which is written in javascript instead of jquery" - this overdrove my oxymoron/contradiction detector. – The Paramagnetic Croissant Aug 31 '14 at 23:52
  • 1
    Sorry, I'm using the wrong terminology. I realize jquery IS javascript. I'm not sure what you call the parentNode and getElementsByTagName parts.... XML? – Lindsay Aug 31 '14 at 23:54
  • 1
    @Lindsay the term you're looking for is native DOM API. – Fabrício Matté Aug 31 '14 at 23:56
  • Thank you Fabricio. Is there any reason something like this would need to be written this way? – Lindsay Aug 31 '14 at 23:57
  • 1
    @Lindsay well, I assume you know [API](http://en.wikipedia.org/wiki/Application_programming_interface) is the term to reference a software component's public operations, methods, types, input and output. And you're talking about [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) methods. So yep, DOM API. – Fabrício Matté Sep 01 '14 at 00:11
  • @Lindsay BTW if you're not familiar with the DOM, see [this thread](http://stackoverflow.com/q/4306870/1331430) and [this answer](http://stackoverflow.com/a/4800664). – Fabrício Matté Sep 01 '14 at 00:22
  • Okay, I'm taking a look at your code -- looks like your code example is missing the part which sets `submitted` to `true` (I'm assuming it'd be in the `submitHandler` option). Also your regex doesn't make much sense to me -- it'd be good to know what you expect to be be `alert`ed. – Fabrício Matté Sep 01 '14 at 00:37
  • Basically, you want to create a `
  • ` element containing the label's text (minus `*`) for each field which fails the validation?
  • – Fabrício Matté Sep 01 '14 at 00:43
  • That is correct. The output should be
  • Label
  • It is working properly unless the p tag is not the direct parent of the input. – Lindsay Sep 01 '14 at 00:56
  • I also would suggest to avoid manipulating HTML strings when possible, see for example [this fiddle](http://jsfiddle.net/9um8xt79/3/) – Fabrício Matté Sep 01 '14 at 00:56
  • Some clean up: http://jsfiddle.net/9um8xt79/4/ – Fabrício Matté Sep 01 '14 at 00:58
  • That's awesome. Thank you. It works if I append it as you said, but I need to wrap it in more HTML before I append it. (It's going into a modal dialog.) Here's a fiddle where I tried appending it, and you'll see it returns as an object? http://jsfiddle.net/9um8xt79/8/ – Lindsay Sep 01 '14 at 01:12
  • I tried changing append to $(summary).html() instead of summary, but that only returned the first label. – Lindsay Sep 01 '14 at 01:21
  • oh sorry I had to be AFK. There are a couple options, e.g. http://jsfiddle.net/9um8xt79/9/ or http://jsfiddle.net/9um8xt79/10/ but it is usually better to keep the separation of concerns (HTML outside of the JS): http://jsfiddle.net/9um8xt79/11/ – Fabrício Matté Sep 01 '14 at 02:16
  • Thank for sticking with it. The first two worked for my needs. If you'd like to move this to an answer I'm happy to mark it correct. Thank you! – Lindsay Sep 01 '14 at 14:10
  • Possible duplicate of [Finding closest element without jQuery](https://stackoverflow.com/questions/18663941/finding-closest-element-without-jquery) – geekley Aug 03 '19 at 23:15