const form = document.getElementById('registrar');
const input = form.querySelector('input');
form.addEventListener ('submit', (e) => {
e.preventDefault(); // preventing the devaul of the object from happening.
const text = input.value; // value takes value of the input value and stores it in text
const ul = document.getElementById('invitedList'); // got to select it first before creating something from it
const li = document.createElement('li');
li.textContent = text; // take the input/values from text then change them into a string using textContent. attach them into li element.
ul.appendChild(li); // append them.
});
I’m just trying to use just straight JS so any input is deeply appreciated (please no libraries or frameworks, those are for later projects. I need to grow in using JS as deeply as possible before I build in the other frameworks/libraries.)
If you don’t intend to submit the form I would change it to a <button type="button"> instead of listening to a submit event and then cancelling it.
form.querySelector('input') may work for now but may or may not work when you have multiple input fields. Better give that field an ID and select that.
Also almost nobody uses document.getElementById anymore AFAIK. Use document.querySelector('#registrar') instead.
Lastly, those comments are fine for now, but you need to ditch them when you get more proficient as they’re really muddying the waters.
lol I’m not sure how you’re using the word “salty” but that is certainly not how I’ve ever heard it used.
But what exactly is wrong with your code? I don’t see anything wrong with this and this is similar to how I would do it.
If you don’t intend to submit the form I would change it to a instead of listening to a submit event and then cancelling it.
I think this is a perfectly valid way of going about it, why do you say it’s bad?
Also almost nobody uses document.getElementById anymore AFAIK.
getElementById is considerably more performant and is definitely still used. I prefer it if I’m selecting by an ID. There’s really not a good reason not to, you’re only saving a few characters.
It just feels off to have code that raises an event (on submit) only to kill immediately when it occurs. To me that sounds like the wrong event was chosen and onclick is a better fit.
Of course, if the submit actually does something server side when Javascript is disabled then the approach is perfectly fine.
It just feels off to have code that raises an event (on submit) only to kill immediately when it occurs. To me that sounds like the wrong event was chosen and onclick is a better fit.
I can see your point, but such is life in JS heavy apps. e.preventDefault() is fairly common. You’re still semantically “submitting” the form, so it’s valid. A lot of times I just don’t use forms at all and gather my data onClick.
I’m only canceling it because when you create the element from the submited input from the user, I want it to show in the browser and not simply refresh. I don’t know of any other way to stop the event from happening without using the prevent default. Can you show me code on what you mean?
I’m trying to learn javascript as deeply as I can by doing the same problem multiple ways. LOL, good comment on the salty thing. Perhaps I’m mistaken, but often I hear the word salty as a software developer slang for that that is elegant (does a lot with few lines). What I am asking is there anyway to make this code more elegant within Javscript?
Could I use prototypes and instances? Could I use a for loop? What ever does the same thing with less lines of code is all I am looking for.
(P.S. I would not reference the Urban dictionary as a reliable resource, lol).
Why are less lines desirable? That tends to make the code more difficult to understand, doesn’t it?
Should the aim be to achieve the most expressive code instead? Frequently that is done with fewer lines than normal, but the goal that achieved it is very different.
I’ve found a good reason. Using getElementById and querySelector results in confusion about why one was used instead of the other. As querySelector is more flexible, sticking with a standard of using just querySelector results in less confusion about things, and later on when you change a unique identifier to a more flexible class name, you don’t need to change getElementById as well.
LOL, good comment on the salty thing. Perhaps I’m mistaken, but often I hear the word salty as a software developer slang for that that is elegant (does a lot with few lines).
Yeah, I’ve never heard that before.
(P.S. I would not reference the Urban dictionary as a reliable resource, lol).
lol Salty is a pretty common gaming term. Most SEs are gamers too. It’s literally a word I say just about every day.
What I am asking is there anyway to make this code more elegant within Javscript?
I honestly don’t think there is much, there’s not really enough here. This is what it would look like if it were mine.
const form = document.getElementById('registrar')
const input = form.querySelector('.someClassNotInput')
const invitedList = document.getElementById('invitedList')
const handleSubmit = e => {
e.preventDefault()
const li = document.createElement('li')
li.textContent = input.value
invitedList.appendChild(li)
}
form.addEventListener('submit', handleSubmit)
K.I.S.S. is better than over engineered stuff that you don’t need. Leave that stuff for when you need it.