Why javascript is so complicated? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why javascript is so complicated?

I just want variable takes each value onclick and to display it onclick.. not just last value.. how to accomplish that?

25th Jan 2017, 6:32 PM
Wolfinho
20 Answers
+ 15
<div class="myclass" onclick="clk(this)" id="1" /> <div class="myclass" onclick="clk(this)" id="2" />…e.t.c.… <script> var i; function clk(e){ document.getElementById("mybtn").innerHTML = e.innerHTML; i = e; } </script>
25th Jan 2017, 6:43 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 14
GIMME CODE!!! ~_~
25th Jan 2017, 7:32 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
(no loops)
25th Jan 2017, 6:48 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
(*edited previous code*)
25th Jan 2017, 6:59 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
It does! ~_~ (what do you want to store in it??)
25th Jan 2017, 7:04 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
With my way, variable "i" contains the element itself so you won't have to do "document.getElementById(i).innerHTML" you could simply do "i.innerHTML"
25th Jan 2017, 7:13 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
Tell me what you wanna do and gimme your code so far… I'll fix it for you… ~_~
25th Jan 2017, 7:18 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
<div id="1" onclick="function(this){i=this.id;}"/> <div id="2" onclick="function(this){i=this.id;}"/> e.t.c.…
25th Jan 2017, 7:19 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
@Louis It is not exactly what i had in mind.. Imagine you have a dropdown list when hover your button "Click me". In dropdown list you have 4 items (for example "click1", "click2", "click3", "click4"). When you click one of these items, a new button will be displayed , next to "Click me" button and it should take the same name (so to say) as in dropdown list (click1, click2 etc.). The name of the button should depend of what item is clicked. When you CLICK a button "Click me", a new button dissapears. I've done everything except that a new button wont to pick its names from dropdown list.
26th Jan 2017, 6:40 AM
Wolfinho
0
function myFunc() { var b=document.getElementsByClassName("myclass"); var i; for(i=0; i<b.length; i++) { document.getElementById("mybtn").innerHTML = b[i].innerHTML; } } there's the damn function. how var i will be 0,1,2,3? depending on which item i click. this way var i = 3 no matter which irem i click. How to make it change to 1, for example, when i click second item?
25th Jan 2017, 6:38 PM
Wolfinho
0
i cant, my unction is named myFunc and it contains other things which change onclick. is there any chance for my variable var i, to change its value inside my function?
25th Jan 2017, 6:55 PM
Wolfinho
0
is there any chance for i variable to change its damn value in myFunc?
25th Jan 2017, 7:04 PM
Wolfinho
0
it is not matter, i have that in myclass. i need only var i to change from 0 to 3, nothing more.
25th Jan 2017, 7:10 PM
Wolfinho
0
for that, i would have to change many elements which have the same onclick function.. i only need: when i click first item of myclass, var i =0; when i click 2nd item of myclass, var i =1; when i click 3rd item of my class, var i =2; when i click 4th item of my class, var i =3;
25th Jan 2017, 7:17 PM
Wolfinho
0
man i already have a function there. i cant change it because one little thing
25th Jan 2017, 7:21 PM
Wolfinho
0
I'm sorry, but if your function is broken or your class is broken: You need to do away with the broken parts and fix it or do away with the whole function/class and try again. Yes, you can do away with a function for one little thing... especially if that one little thing is keeping you from completing your task. If I understand what you're wanting: You're looking for this.... https://jsfiddle.net/y6weyp7L/6/
25th Jan 2017, 7:48 PM
Louis Milotte
Louis Milotte - avatar
0
thanks man, i will try it tommorow. i've lost too many nervs with javascript these days, today especially. the worst thing is - i was so close yesterday, it displayed me 3/4 values, every except the first one, and me, delete it.. now cant remember, what i ve done!!!
25th Jan 2017, 7:56 PM
Wolfinho
0
I've made it! after 7 days of battle, i've made it. Instead of 1 effective function I have 4 functions, but it works. I will have to write all other stuff *4, but who cares?! :-) I'mnot smart enough to put everything in few lines of code, but i'll make it. Button is defeated! :-D Cheers! :-D
26th Jan 2017, 9:12 AM
Wolfinho
- 1
and that will create loop? Will it choose var i =0, when i click first item?
25th Jan 2017, 6:46 PM
Wolfinho
- 1
nope.
25th Jan 2017, 7:03 PM
Wolfinho