formData in web-components
Hello, I made a web component:
This is the html:
And it works, the form element recognizes
my-toggle
as a member of the form. But console.log(new FormData(form))
gives me an empty FormData object.
Why?17 Replies
instead of adding html, try creating the element using document.createElement
but honestly, if you want to make a toggle, i would just do a label with styles
I need more than one element to make a toggle, thats why I wanted to use a custom element
or you can just set the appearence: none on the checkbox, and now you can style with a ::before and ::after
Why?
and its all 1 element
because adding html like that may have unintended side effects
does it have? i dont know
I use
innerHTML
because I want to be able to write readable html instead of calling dozens of methods
But I'll try using :before and :after and get rid of the componentit will be more convient and this type of work
trust me, it is a lot easier
you can set the checkbox to have
appearance: none
with a height, width, border-radius and background
the ::before can have a height that's 100% height - 2x the width of the border
and have 50% border radius
and then you can just transition the background and position of the ::beforehere is the code
<!DOCTYPE html>
<html>
<style>
.myinput {
appearance: none;
display: inline-block;
position: relative;
width: 20px;
height: 20px;
border: 2px solid gray;
border-radius: 4px;
transition: background-color, border-color ease-in 0.2s;
cursor: pointer;
}
.myinput::before {
content: '';
display: none;
position: absolute;
top: 1px;
left: 50%;
translate: -50% 0;
height: 60%;
width: 6px;
border-right: 2px solid white;
border-bottom: 2px solid white;
transform: rotate(45deg);
transition: display ease-in 0.2s;
}
.myinput:hover {
border-color: lightGray;
}
.myinput:checked {
background: green;
}
.myinput:checked::before {
display: inline-block;
} </style> <body> <form> <input class="myinput" type="checkbox"/> </form> </body> </html> you can do more using html data attribute
} </style> <body> <form> <input class="myinput" type="checkbox"/> </form> </body> </html> you can do more using html data attribute
display doesnt transition: it jumps from "none" to "block"
yup I already almost have it
and this way, you dont have to try to shoehorn your custom element into the formdata
Thanks, using a web-component was, as so often, the wrong way
my bad, Instead I use opacity
What do you need opacity for anyway?
transitioning from hidden to visible
also, avoid using transition without properties
that means that everything will transition, when the css loads
including width, height and other properties that cause layout shifting
good advise