trying to understand forms

trying to understand forms through mdn
51 Replies
bfmv
bfmvOP4mo ago
at this point, i think this is more about trying to understand mdn rather than forms like im genuinely curious wait let me share some things written in it
13eck
13eck4mo ago
What’s your code look like? What isn’t working? What do you think it should be doing?
bfmv
bfmvOP4mo ago
i didnt even understand what i have to write💀 take a look at this
This element includes the global attributes.

accept Deprecated
Comma-separated content types the server accepts.

Note: This attribute has been deprecated and should not be used. Instead, use the accept attribute on <input type=file> elements.

accept-charset
The character encoding accepted by the server. The specification allows a single case-insensitive value of "UTF-8", reflecting the ubiquity of this encoding (historically multiple character encodings could be specified as a comma-separated or space-separated list).

autocapitalize
Controls whether inputted text is automatically capitalized and, if so, in what manner. See the autocapitalize global attribute page for more information.

autocomplete
Indicates whether input elements can by default have their values automatically completed by the browser. autocomplete attributes on form elements override it on <form>. Possible values:

off: The browser may not automatically complete entries. (Browsers tend to ignore this for suspected login forms; see Managing autofill for login fields.)
on: The browser may automatically complete entries.
name
The name of the form. The value must not be the empty string, and must be unique among the form elements in the forms collection that it is in, if any.

rel
Controls the annotations and what kinds of links the form creates. Annotations include external, nofollow, opener, noopener, and noreferrer. Link types include help, prev, next, search, and license. The rel value is a space-separated list of these enumerated values.
This element includes the global attributes.

accept Deprecated
Comma-separated content types the server accepts.

Note: This attribute has been deprecated and should not be used. Instead, use the accept attribute on <input type=file> elements.

accept-charset
The character encoding accepted by the server. The specification allows a single case-insensitive value of "UTF-8", reflecting the ubiquity of this encoding (historically multiple character encodings could be specified as a comma-separated or space-separated list).

autocapitalize
Controls whether inputted text is automatically capitalized and, if so, in what manner. See the autocapitalize global attribute page for more information.

autocomplete
Indicates whether input elements can by default have their values automatically completed by the browser. autocomplete attributes on form elements override it on <form>. Possible values:

off: The browser may not automatically complete entries. (Browsers tend to ignore this for suspected login forms; see Managing autofill for login fields.)
on: The browser may automatically complete entries.
name
The name of the form. The value must not be the empty string, and must be unique among the form elements in the forms collection that it is in, if any.

rel
Controls the annotations and what kinds of links the form creates. Annotations include external, nofollow, opener, noopener, and noreferrer. Link types include help, prev, next, search, and license. The rel value is a space-separated list of these enumerated values.
this is the first instruction given in the mdn about the form element like, this is just theoretical knowledge about the different attributes of form what do i do now in w3 school, the instructions were more practical and straight to the point
ἔρως
ἔρως4mo ago
that's the very very technical part of it those are the attributes that the element accepts
bfmv
bfmvOP4mo ago
how do i even know which ones to use😭
bfmv
bfmvOP4mo ago
MDN Web Docs
<form>: The Form element - HTML: HyperText Markup Language | MDN
The <form> HTML element represents a document section containing interactive controls for submitting information.
ἔρως
ἔρως4mo ago
well, easy: - use the global ones when you need - don't use the deprecated ones - use the ones that do what you want to do or that help you get there
bfmv
bfmvOP4mo ago
i think im approaching mdn in a wrong way
ἔρως
ἔρως4mo ago
what are you even trying to do?
bfmv
bfmvOP4mo ago
im seeing it as a go-to website for learning webdev since i assumed that thats what people suggested me to do im not sure if thats what they told me
ἔρως
ἔρως4mo ago
dude what are you trying to do? where's the code? what's wrong with it?
bfmv
bfmvOP4mo ago
im trying to learn about forms same as yesterday
ἔρως
ἔρως4mo ago
what for? and did you ever write any code yet?
ἔρως
ἔρως4mo ago
i don't see a form there
bfmv
bfmvOP4mo ago
after doing this, i thought to redo it using form and <template> and thats why today i thought to learn about forms and couldnt understand anything
ἔρως
ἔρως4mo ago
alright what do you want to do with the form?
bfmv
bfmvOP4mo ago
but when i use youtube or any other website to learn about forms, i understand the same thing that i did in the codepen, since forms are the correct way to do it u told me
ἔρως
ἔρως4mo ago
no no tell me what are you trying to do? i want you to think about it it doesn't matter how much you read and study about it if you don't know wtf you will do
bfmv
bfmvOP4mo ago
i want to be able to work with forms through javascript reading the input value etc
ἔρως
ἔρως4mo ago
okay, did you checked the examples?
bfmv
bfmvOP4mo ago
yeah
ἔρως
ἔρως4mo ago
did you understand anything?
bfmv
bfmvOP4mo ago
it was html/css only yeah
ἔρως
ἔρως4mo ago
then start from there make a form out of it forget javascript for now
bfmv
bfmvOP4mo ago
<form id="box-add" method="get">
<input maxlength="50" id="task-input" type="text" placeholder="Add your task">
<button id="add-btn">ADD</button>
</form>
<form id="box-add" method="get">
<input maxlength="50" id="task-input" type="text" placeholder="Add your task">
<button id="add-btn">ADD</button>
</form>
isnt this it
ἔρως
ἔρως4mo ago
almost
bfmv
bfmvOP4mo ago
and adding the action attribute in the form
ἔρως
ἔρως4mo ago
you usually do need that, yes your button also needs the type
bfmv
bfmvOP4mo ago
in the example, they dont show it
<!-- Form which will send a GET request to the current URL -->
<form method="get">
<label>
Name:
<input name="submitted-name" autocomplete="name" />
</label>
<button>Save</button>
</form>

<!-- Form which will send a POST request to the current URL -->
<form method="post">
<label>
Name:
<input name="submitted-name" autocomplete="name" />
</label>
<button>Save</button>
</form>

<!-- Form with fieldset, legend, and label -->
<form method="post">
<fieldset>
<legend>Do you agree to the terms?</legend>
<label><input type="radio" name="radio" value="yes" /> Yes</label>
<label><input type="radio" name="radio" value="no" /> No</label>
</fieldset>
</form>
<!-- Form which will send a GET request to the current URL -->
<form method="get">
<label>
Name:
<input name="submitted-name" autocomplete="name" />
</label>
<button>Save</button>
</form>

<!-- Form which will send a POST request to the current URL -->
<form method="post">
<label>
Name:
<input name="submitted-name" autocomplete="name" />
</label>
<button>Save</button>
</form>

<!-- Form with fieldset, legend, and label -->
<form method="post">
<fieldset>
<legend>Do you agree to the terms?</legend>
<label><input type="radio" name="radio" value="yes" /> Yes</label>
<label><input type="radio" name="radio" value="no" /> No</label>
</fieldset>
</form>
ἔρως
ἔρως4mo ago
because the default is submit but you should be explicit about everything
bfmv
bfmvOP4mo ago
ohk is web.dev a good source to learn it seems so much easier
ἔρως
ἔρως4mo ago
never used that
13eck
13eck4mo ago
Not bad, but seems to focus a lot on Python. And from listening to their podcast they’re not front end people so any FE content needs to be seen with some skepticism
bfmv
bfmvOP4mo ago
what did u use as a beginner to learn web dev
ἔρως
ἔρως4mo ago
stackoverflow, when it was good trial and error then mdn
13eck
13eck4mo ago
Let’s keep this thread on-topic please If you want to discuss learning platforms check #resources or make a discussion post
bfmv
bfmvOP4mo ago
actually the intent for this post was both should i change the topic is it fine
13eck
13eck4mo ago
One post, one question
bfmv
bfmvOP4mo ago
https://codepen.io/Designer-the-encoder/pen/gbbyjBb
addBtn.addEventListener("click", () => {
let taskListItem = document.querySelector(".task-list-item")
let clone = taskListItem.cloneNode(true)
clone.style.display = "flex"

let checkBoxElement = clone.querySelector("input")
let pElement = clone.querySelector("p")
let removeBtn = clone.querySelector(".remove-btn")
let taskName = taskInput.value


removeBtn.addEventListener("click", () => {
taskListContainer.removeChild(clone)
})
checkBoxElement.addEventListener("click", () => {
if (checkBoxElement.checked === true) {
pElement.style.textDecoration = "line-through"
}
else(
pElement.style.textDecoration = "none"
)
})

pElement.textContent = taskName
taskListContainer.appendChild(clone)
taskInput.value = ""
})
addBtn.addEventListener("click", () => {
let taskListItem = document.querySelector(".task-list-item")
let clone = taskListItem.cloneNode(true)
clone.style.display = "flex"

let checkBoxElement = clone.querySelector("input")
let pElement = clone.querySelector("p")
let removeBtn = clone.querySelector(".remove-btn")
let taskName = taskInput.value


removeBtn.addEventListener("click", () => {
taskListContainer.removeChild(clone)
})
checkBoxElement.addEventListener("click", () => {
if (checkBoxElement.checked === true) {
pElement.style.textDecoration = "line-through"
}
else(
pElement.style.textDecoration = "none"
)
})

pElement.textContent = taskName
taskListContainer.appendChild(clone)
taskInput.value = ""
})
to integrate form into this project, it seems that i only had to change to div element to form element because the js seems to remain the same, do i need to change anything
ἔρως
ἔρως4mo ago
instead of grabbing the elements manually, you can use a formdata also, you have to listen to the submit event, not the click
bfmv
bfmvOP4mo ago
ohk
13eck
13eck4mo ago
In order for there to be a form submit event there needs to be a form element. And there's not
bfmv
bfmvOP4mo ago
oh wait thats the outdated code
13eck
13eck4mo ago
Hopefully this minimal example will show you how forms work
bfmv
bfmvOP4mo ago
omggggg thanks when we dont use " event.preventDefault();" on forms addeventlistener, the page reloads the moment we click submit so it means that its always a necessity to use it on forms, right? are there any cases where we dont use " event.preventDefault();"
13eck
13eck4mo ago
The default is for the form to redirect navigate to a new page. If you don't want that you need to prevent the default from happening
ἔρως
ἔρως4mo ago
not redirect: navigate. redirects are different and function differently, and may or may not lose the submitted form data i know im being annoying with my correction, but both are indeed different as the name of the method suggests, it cancels the default action that would have happened otherwise for example, on a link it would have been to navigate to a new page or on a file input would be to show the file selection usually, you dont use that method on click, change, input, search and resize events there are more events
bfmv
bfmvOP4mo ago
ohh so whether we use " event.preventDefault(); " or not depends on the type of event?
13eck
13eck4mo ago
It depends on if you want to let the default happen or not. It's not about the event, is about the outcome you want

Did you find this page helpful?