0
Why for loop takes only last value?
function myFunc() { var b=document.getElementsByClassName("myclass"); var i; for(i=0; i<b.length; i++) { document.getElementById("mybtn").innerHTML = b[i].innerHTML; } } That's my code. I want to asign a different value to my button every time it is clicked and it displays only the last. /* or displays nothing, depending of its will */.Where I'm wrong?
12 Respuestas
+ 3
Lol bro,
for (var i = 0; i < 5; i++) {
document.getElementById("mybtn").innerHTML = i
}
this will mean that the innerHTML of your button will be changed 5 times, in the end it will display 4.
If you wanna assign a different value, do this,
document.getElementById("mybtn").onclick = function() {
document.getElementById("mybtn").innerHTML = document.getElementById("mybtn").innerHTML * 1 + 1
}
+ 3
What do you mean? Can you elaborate on that sentence?
+ 3
Ok so what values do you want the button to be reassigned? Lentin pls help.
+ 3
Do you have like a code so I kind of get what you are trying to do? At least post a code
..
0
how to write it to display each value?
if i dont use for loop and set var i = 0, it displays 1st value, as expected.. how to assign new value for i var, other than with loop? And for loop goes through each iteration. i'm totally confused.. tried over and over, with different variations and nothing.. I'm so stupid for js..
0
but that is onclick function for mybtn already. and how it will know what to write if there's no myclass.innerHTML which contains the values?
0
These values:
i have a button called Pictures. When I hover pictures it opens dropdown list containing Spring, Summer, Autumn, Winter. When I click one of these from dropdown, i want a new button to show up next to Pictures called the same as in dropdown.
0
the code is too big, but is working, it takes a the last value. once i get to change them each time i click, but without first value. i deleted it and know dont remember what i've done.. my class contains items from dropdown list (spring, summer, autumn, winter). mybtn is the new button which displays when i click one of items. its name is the problem. i want it to call summer, when i click summer from dropdown, winter when i click winter etc..
0
I'll just give up. so small thing and i'm having headaches for days.. i'm definitely not for js.. we just dont understand each other, me and js.. how i'll manage more complicated behaviour when i'm having troubles with button. heeey button! sometimes, it is the best to accept the truth..
0
just want to var i takes every value from 0 to 3 in my script and it wont. anybody knows how to write it? where to put loop? or whatever, no matter if it needs to be written 100 times more than necessary, just to work?!
when i click 1st item from dropdown, i want var i =0;
when i click 2nd item, i want var i = 1;
3rd item, var i = 2;
4th item, var i = 3.
How to accomplish that?
Now it takes only last value. Where to put loop in order to take every value?
0
so freakin annoying
0
@cheeze
I've beat the button. :-P although I had to write 4 functions instead of 1. But, while I was writing my huge functions, I noticed how I could short that by some reordering of classes/ids. Influenced by Google searching for my questions, I noticed that 90% of them are in jQ, so I finished it quickly. While learning, I saw at least 5 things I was wondering about how to do, which I could easily implement into my page and that will provide me even more simple script.