Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

another fascinating way to use an array?

I found this as I'm giving my brain a break, perusing through some beginner courses:

<input name="pet[]" type="text">

I gather that the array "pet" stores input that the user types in. What's striking to me, it seems it's not just about learning stuff... it's about finding innovative ways to use this stuff. So if I have a question I guess it would be...is this a typical thing? or was someone being inventive?

3 Answers

Erik Nuber
Erik Nuber
20,629 Points

Would be interesting to see where you saw that. According to W3Schools, the name field is supposed to contain only text. And the mozilla developers network says the anme field is "the name of the control, which is submitted with the form data."

I can't find anything that suggests you can use an array in this way. But I could be wrong.

OK, that that means someone is being inventive. Cause the code works perfectly and it's a treehouse course. So, very cool.

Erik Nuber
Erik Nuber
20,629 Points

Can you link me to the course. I would like to see that. I am wondering if it is being read as just text or if an array is really being formed.

Steven Parker
Steven Parker
229,981 Points

:point_right: The name attribute is just text. The brackets are part of the text.

The normal function of name in the submit process is circumvented here by a JavaScript handler. The name is then used in a querySelectorAll call to identify the inputs in a particular group and assign a variable named "inputs". No array with the name shown is ever constructed in the program. Assuming the HTML and JavaScript are kept in sync, the name could be changed to anything you like (with or without brackets or other symbols) and it would have no impact on the program operation.

I don't recall it ever being discussed in the course, but my guess is the brackets are added to the name only as a visual reminder that the input is part of a group.

This example comes from the novice-level course Treehouse Club - MASH.

Steven, I'm going to keep going through the code to understand what you said, cause of yet it's all like pudding in my mind. I may have another question once I grasp enough of what you said to actually formulate another thought.

Kevin Korte
Kevin Korte
28,148 Points

Rails forms have been doing something like this for a long time. Just scroll through and look at the output boxes and you'll see the html that Rails is generating.

http://guides.rubyonrails.org/form_helpers.html

Kevin, as an aside; I noticed you do both ruby and php. What do you perceive as advantages of one over the other?

Kevin Korte
Kevin Korte
28,148 Points

That's a good question, I don't have a great answer for it. I've sinced jump heavily in javascript applications, node base, and I can say that it seems to be easier to pass data back and forth between client and server, but it's still not without some things you give up.

In regards to php/ruby. Today, both are fantastic options. Php has been revitalized lately, and so there are a lot of cool tools and options. This is personal, but I much prefer the syntax of Ruby over the syntax of PHP. To me it's cleaner, more readable, and less punctuation (dang those missing semicolons!).

However, php has a much lower bar to actually launching a website. It's very easy, and almost any host supports PHP, the same isn't always true about Ruby.

I still prefer Ruby over Php only because of syntax.