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
lko6mo ago
please send your code with ```js
محمد
محمدOP6mo ago
sorry i didn't understand
lko
lko6mo ago
```js code ``` Ayways what do you think this do?
numberOfElements.addEventListener("input",() => numberOfElements.value)
numberOfElements.addEventListener("input",() => numberOfElements.value)
محمد
محمدOP6mo ago
i have an input in the html and this should get its value
محمد
محمدOP6mo ago
I'm trying to do like this
No description
lko
lko6mo 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
محمد
محمدOP6mo ago
yes that's why there's a .value at the end of it so i only get the value out of it
lko
lko6mo ago
Yes but it doesnt return the value
محمد
محمدOP6mo ago
why
lko
lko6mo ago
because addEventListener just creates a listener, doesnt return anything
محمد
محمدOP6mo 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
lko6mo 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)
}
})
محمد
محمدOP6mo ago
is this possible ???
lko
lko6mo ago
yep, you can just take the value by doing numberOfElements.value
محمد
محمدOP6mo ago
it's not working
lko
lko6mo ago
does it give you an error?
محمد
محمدOP6mo ago
yes
lko
lko6mo ago
what
محمد
محمدOP6mo ago
No description
lko
lko6mo ago
it's just missing a ) at line 29 I dont have the full code so I cant help you
محمد
محمدOP6mo ago
no i just added the => ok there's another problem
محمد
محمدOP6mo ago
No description
lko
lko6mo ago
const numberOfElements2 = numberOfElements.value
for (let i = 0; i < numberOfElements2; i++) {
const numberOfElements2 = numberOfElements.value
for (let i = 0; i < numberOfElements2; i++) {
محمد
محمدOP6mo ago
OHHHHHHHHHHHHMYYYYYYYYGOOOOOOOOOOD IT WORKED THANK YOU SIR
lko
lko6mo ago
Do you understand what it did? and how it works
محمد
محمدOP6mo ago
probably yes
Want results from more Discord servers?
Add your server