Search code examples
javascriptjqueryformsoverflow

Why does JavaScript go into never ending loop and not just error out on input send


UPDATE Evidently I am wrong in that it's vanilla JS -- IT's jQuery only that is affected this way.

I know that it DOES to it. But everywhere I go gives you instructions on how to send a form properly. That's not what I am looking for.

I am wondering that why when you make the mistake of trying to (I realize I am using jQuery here, but there is no difference in this, or getElementByID)

$.post("/scripts/intake.php", {
        full_name: $('#full_name'),
        ...

Why does JavaScript even allow a "raw element" to be "sent"? But my real question is, what actually causes the problem? Is it looping infinitely through the DOM Object (which is what I am suspecting)? Can someone explain what happens specifically that JS freaks out over sending a DOM Object VS sending a parameter of said Object:

full_name: $('#full_name').val()

I understand that Object would be useless to the page posted to .. But what happens here to make JavaScript error out in an overflow vs just implementing an internal "try - catch" to prevent the nuance of waiting for it to time out, only to realize you have bad syntax?


Solution

  • The problem is jquery is trying to traverse the object and all of its properties, the problem is that it will eventually reach back to itself during the traversal.

    Eg. Each element has a parent node property and that parent node property has a children nodes property (which contains the original element) which will cause an infinite traversal.