CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Equivalent of form.elements  (Read 55 times)

AndrewTraub

  • Jr. Member
  • **
  • Posts: 66
  • Karma: +0/-0
Equivalent of form.elements
« on: 29 Dec 2022, 09:28:56 am »
Is there an equivalent to form.elements in which I can pass in a fieldset id of a form element and only return that section's elements?

AndrewTraub

  • Jr. Member
  • **
  • Posts: 66
  • Karma: +0/-0
Re: Equivalent of form.elements
« Reply #1 on: 29 Dec 2022, 09:41:25 am »
Seems that container.getElementsByTagName('input') works.

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 871
  • Karma: +166/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Equivalent of form.elements
« Reply #2 on: 29 Dec 2022, 02:41:03 pm »
note that only returns inputs, so if you have any textarea or select, you have to do multiple passes. This is why they recently gave us querySelectorAll.

container.querySelectorAll("input, textarea, select")

Will give you an iterable of all three tags.

getElementById, getElementsByClassName, and getElementsByTagName are fastest when you're looking for single matches. querySelector and querySelectorAll are "better" when you want to grab bunch of complex lookups possible results.

You might not even need the container. Let's say your container was <fieldset id="about">

aboutElements = document.querySelectorAll("#about input, #about textarea, #about select");

Fun stuff.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 871
  • Karma: +166/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Equivalent of form.elements
« Reply #3 on: 29 Dec 2022, 02:43:39 pm »
Oh, and if your form is well-formed and said container is a fieldset, you can skip all that as fieldsets have their own HTMLFieldSetElement.elements property.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

 

SMF spam blocked by CleanTalk

Advertisement