Weird code function

i have a problem with the i < (long code) the problem is it doesn't take the number so for example if a put it to i < 4 it works but the other way doesn't here's the code: <form action=""> <input type="number" name="elements" class="input" placeholder="Number Of Elements" /> <input type="text" name="texts" class="input" placeholder="Elements Text" /> <select name="type" class="input"> <option value="Div">Div</option> <option value="Section">Section</option> </select> <input type="submit" name="create" value="Create" /> <div class="results"></div> </form>
let numberOfElements = document.querySelector("[name='elements'")
let elementsText = document.querySelector(`[name='texts'`)
let elementsType = document.querySelector("[name='type'")
let createBtn = document.querySelector("[name='create'")
let results = document.querySelector(".results")
// Creation

let valueHolder = numberOfElements.addEventListener("input",() => numberOfElements.value)


createBtn.addEventListener("click",function (preventor) {
for (let i = 0 ;i < numberOfElements.addEventListener("input",() => numberOfElements.value) ;i++) {
let divCreation = document.createElement("div")
results.appendChild(divCreation)
}
preventor.preventDefault()
}
)
let numberOfElements = document.querySelector("[name='elements'")
let elementsText = document.querySelector(`[name='texts'`)
let elementsType = document.querySelector("[name='type'")
let createBtn = document.querySelector("[name='create'")
let results = document.querySelector(".results")
// Creation

let valueHolder = numberOfElements.addEventListener("input",() => numberOfElements.value)


createBtn.addEventListener("click",function (preventor) {
for (let i = 0 ;i < numberOfElements.addEventListener("input",() => numberOfElements.value) ;i++) {
let divCreation = document.createElement("div")
results.appendChild(divCreation)
}
preventor.preventDefault()
}
)
26 Replies
lko
lko5mo ago
please send your code with ```js
محمد
محمدOP5mo ago
sorry i didn't understand
lko
lko5mo ago
```js code ``` Ayways what do you think this do?
numberOfElements.addEventListener("input",() => numberOfElements.value)
numberOfElements.addEventListener("input",() => numberOfElements.value)
محمد
محمدOP5mo ago
i have an input in the html and this should get its value
محمد
محمدOP5mo ago
I'm trying to do like this
No description
lko
lko5mo ago
No, .addEventlistener creates a listener, in your case of the input, so you're basically saying if i is less than listener, which doesnt make any sense, listener is not a number
محمد
محمدOP5mo ago
yes that's why there's a .value at the end of it so i only get the value out of it
lko
lko5mo ago
Yes but it doesnt return the value
محمد
محمدOP5mo ago
why
lko
lko5mo ago
because addEventListener just creates a listener, doesnt return anything
محمد
محمدOP5mo ago
ok then do i do it i need a method that gives me the live value of the input and addEventListener is the only one who can do it like this
lko
lko5mo ago
let numberOfElements = document.querySelector("[name='elements']") // added missing ']'
let elementsText = document.querySelector(`[name='texts']`) // again
let elementsType = document.querySelector("[name='type']") // again
let createBtn = document.querySelector("[name='create']") // again
let results = document.querySelector(".results")

createBtn.addEventListener("click", (e) {
e.preventDefault()
const numberOfElements = numberOfElements.value
for (let i = 0; i < numberOfElements; i++) {
let divCreation = document.createElement("div")
results.appendChild(divCreation)
}
})
let numberOfElements = document.querySelector("[name='elements']") // added missing ']'
let elementsText = document.querySelector(`[name='texts']`) // again
let elementsType = document.querySelector("[name='type']") // again
let createBtn = document.querySelector("[name='create']") // again
let results = document.querySelector(".results")

createBtn.addEventListener("click", (e) {
e.preventDefault()
const numberOfElements = numberOfElements.value
for (let i = 0; i < numberOfElements; i++) {
let divCreation = document.createElement("div")
results.appendChild(divCreation)
}
})
محمد
محمدOP5mo ago
is this possible ???
lko
lko5mo ago
yep, you can just take the value by doing numberOfElements.value
محمد
محمدOP5mo ago
it's not working
lko
lko5mo ago
does it give you an error?
محمد
محمدOP5mo ago
yes
lko
lko5mo ago
what
محمد
محمدOP5mo ago
No description
lko
lko5mo ago
it's just missing a ) at line 29 I dont have the full code so I cant help you
محمد
محمدOP5mo ago
no i just added the => ok there's another problem
محمد
محمدOP5mo ago
No description
lko
lko5mo ago
const numberOfElements2 = numberOfElements.value
for (let i = 0; i < numberOfElements2; i++) {
const numberOfElements2 = numberOfElements.value
for (let i = 0; i < numberOfElements2; i++) {
محمد
محمدOP5mo ago
OHHHHHHHHHHHHMYYYYYYYYGOOOOOOOOOOD IT WORKED THANK YOU SIR
lko
lko5mo ago
Do you understand what it did? and how it works
محمد
محمدOP5mo ago
probably yes
Want results from more Discord servers?
Add your server