Locating nearest section id if it exists

Locating nearest section id if it exists

I need to pass the id of the nearest "section" if it exists. If a section doesn't exist, I need to pass the "formPlace" hidden field. If that doesn't exist, I need to pass the URL of the page. How can I do that with jQuery?

  1. <section id="contactForm">
  2. <form...
  3. <input type="hidden" name="formPlace" id="formPlace" value="contact1234">
  4. ...
  5. </form>
  6. </section>