+ 13

[fixed] Onclick not working

I wanted to change image and text when user clicks a button. But nothing happens after clicking button 😢 Why ? https://code.sololearn.com/Wcqmj0SjU7yR/?ref=app

16th Sep 2020, 11:42 AM
Prasant
Prasant - avatar
44 odpowiedzi
+ 33
"click" function name doesn't work. Please rename your function. Hope it works. Here is the explanation: https://stackoverflow.com/questions/4388443/javascript-function-name-cannot-set-as-click#:~:text=Because%20there's%20a%20.
16th Sep 2020, 11:47 AM
NightFox
16th Sep 2020, 11:48 AM
Aditya
Aditya - avatar
+ 17
The 'click' by itself is NOT a javascript reserved keyword outside the context of the inline event handler . That is to say the "click" method defined in the question is valid. //I hope this works Try by using event handler 'externally' will work. E.g. using jQuery: $('input').click(click);
16th Sep 2020, 11:55 AM
Aditya
Aditya - avatar
+ 15
wlcm :D
16th Sep 2020, 11:58 AM
Aditya
Aditya - avatar
+ 9
Change the name of the click function Try anything else other than click and it will work
16th Sep 2020, 11:46 AM
Ruba Kh
Ruba Kh - avatar
+ 7
For Cross-platform capabilities, try using this function that I, Kode Krasher and a few others brought up and have been using every since, main credits to Kode krasher const eventBasedOnDevice = navigator.userAgent.match(/ipod|ipad|iphone/i) ? 'touchstart' : 'click'; document.getElementById(/*id of whatever you want to check for clicks/*).addEventListener(eventBasedOnDevice, function event(event) { touchHandler(); //what you want to happen onclick })
16th Sep 2020, 12:55 PM
Vachila64☕
Vachila64☕ - avatar
+ 5
Ruba Kh , Satnam , Aditya Thank you all.😊 But is "click" a reserved word for JavaScript ?
16th Sep 2020, 11:51 AM
Prasant
Prasant - avatar
+ 5
Kode Krasher Is the line navigator.userAgent.match(/ipod|ipad|iphone/i) to know whether the user device is Android/ iPhone/ other ?
16th Sep 2020, 2:12 PM
Prasant
Prasant - avatar
+ 5
Kode Krasher Hmm I could do that after a long experiment 😅😅 https://code.sololearn.com/WB2xjlW46c3c/?ref=app
16th Sep 2020, 5:10 PM
Prasant
Prasant - avatar
+ 4
Aditya Oh I got it Thank you very much again 😊
16th Sep 2020, 11:58 AM
Prasant
Prasant - avatar
+ 4
Sofi💞 Thank you 😊 I got it now
16th Sep 2020, 12:12 PM
Prasant
Prasant - avatar
+ 4
Kode Krasher Thank you very much Sir for explaining it
16th Sep 2020, 2:50 PM
Prasant
Prasant - avatar
+ 4
مهان علوی مجد But I don't know what is eventlistner, as I have not completed JavaScript 😔. Can you please give an example how it works ?
17th Sep 2020, 11:07 AM
Prasant
Prasant - avatar
+ 4
Kode Krasher Thank you I was also thinking to use arrays, so that we can use many images as we wish. But I couldn't understand this if ( theMsg === messages[1] ) How does this count the number of clicks or how does it work ?
18th Sep 2020, 2:27 AM
Prasant
Prasant - avatar
+ 3
Vachila64☕ Would you please give an example ? (code) I couldn't understand that function 😢
16th Sep 2020, 1:23 PM
Prasant
Prasant - avatar
+ 3
Kode Krasher I have not tried yet 😅 Let me try first
16th Sep 2020, 3:40 PM
Prasant
Prasant - avatar
+ 3
change name of function to clickme for example. you can't use "click" for name of a function.
17th Sep 2020, 4:23 AM
Valeh
Valeh - avatar
+ 3
Kode Krasher Again I had never tried such projects 😅. But let me try once.
17th Sep 2020, 11:05 AM
Prasant
Prasant - avatar
+ 3
Kode Krasher Thank you again 😊
17th Sep 2020, 2:48 PM
Prasant
Prasant - avatar
+ 3
Kode Krasher Oh now I got it First it checks whether (theMsg === messages[1]) or not Then it returns false, so it exicutes else statement and changes to to Second image and text. When we press the button again, the if condition returns true and again changes to first image and text. In this way the loop continues. Am I right ?
18th Sep 2020, 5:56 AM
Prasant
Prasant - avatar