My recursive function isn't returning anything

Jonny Barnes

I'm trying to select a the parent form element of an input. The form element isn't necessarily the direct parent node. Currently this outputs "undefined" to my log.

var anInputElement = document.querySelector(...);
var formElement = getFormElement(anInputElement);
console.log(formElement);

function getFormElement(elem) {
  //if we've traversed as high as the `body` node then
  //we aint finding the `form` node
  if(elem.nodeName.toLowerCase() !== 'body') {
    var parent = elem.parentNode;
    if(parent.nodeName.toLowerCase() === 'form') {
      return parent;
    } else {
      getFormElement(parent);
    }
  } else {
    return false;
  }
}

Why am I getting undefined in my console log?

Adam

not just

getFormElement(parent);

but

return getFormElement(parent);

and simplified, just for fun:

function getFormElement(elem) {
  if(elem.nodeName.toLowerCase() !== 'body') {
    var parent = elem.parentNode;
    return parent.nodeName.toLowerCase() === 'form' ? parent : getFormElement(parent);
  }

  return false;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My PHP form isn't returning anything from $_POST

Why is my recursive binary search function not returning anything?

JSX isn't returning anything

React JS: ForEach Function isn't returning anything

Why isn't this processing function returning anything related to the string?

Why isn't my function returning the string?

Why isn't my function returning?

javascript selector with outerheight function isn't returning anything, and I can't select the selector anywhere else either

Why isn't setInterval() returning my function's return value?

My function isn't returning the right type, is it possible?

Returning error message for my function if the argument isn't relevant

Why isn't my function returning the correct Key of the values?

Why isn't my application drawing anything?

My PHP file isn't showing anything?

My Javascript isn't printing anything

Clear function isn't doing anything

My python function is supposed to work but it is not returning anything

Why is my query in CodeIgniter function not returning anything?

Why is my second function not returning anything?

PHP PDO while loop isn't returning anything

Django queryset in view isn't returning anything to the html template

Why isn't my output returning as expected?

Why isn't a function tail recursive?

Why isn't this function tail recursive?

Why isn't the return function inside my recursive call being executed?

My function isn't working

My Function isn't working?

Why is my recursive function returning undefined?

Why is my recursive JavaScript function not returning the string?