+ 1
setting an alert as a non-click
https://code.sololearn.com/WY3YSou8oqRK/#js I want the script to set an alert as an onclick, but it alerts onload, and then nothing happens onclick. Line41 in js
6 ответов
+ 3
https://code.sololearn.com/WDZAc8i4riMY/?ref=app
OK instead of editing my first answer again, I should post a third answer to make it clear that this is newly added content. Again I have to say sorry because I posted soemthing before I actually fixed your code and cause confusion.
The problem is three steps:
Firstly you spelt wrongly
It's not onlick, it should be onclick
Secondly you run popup because of the (), should be no () because you are assigning callback.
Thirdly you try to pass argument to mosue event handler. Note that all argument will be overwritten by the mouse event as argument. Therefore passing I into the anonymous function won't work.
Fourthly using var tempString to get the string first for inner function to alert won't work, because it's a closure and tempString is rememberd and only the last string will be printed for any row clicked. The solution is to use block scope declaration with ES6 keyword let.
+ 3
1. JS line 41:
td.onlick = popup(I);
With (), popup is triggered, and hence alert() in popup is executed. If it is :
td.onclick = popup;
This won't trigger the popup. In the same time, because you want to use the index of for loop to set different contents for alert, you'll need to use anonymous function so it'll find the right index of the array each assignment.
You can try first, if you can't work it out, a correction is here:
2. HTML line 11
<body onload="Smith()">
No need the onload attribute
+ 3
You can try changing the let keyword in my corrections with var. And then you'll see that all boxes print the same result.
A very detailed explanation is in Morpheus JS Fact Series, way before the Sololearn ES6 lessons are added to JS course.
https://www.sololearn.com/post/39014/?ref=app
+ 2
The problem is with the (), sorry my previous answer not detailed. The demo will be done in 0.000001 millisecond.
0
I originally had popup just alert, without the assignment; same issue.
I removed the onload property; same issue. It only broke my display.
0
Brilliant. Thank you so much. Ill have to finish going through the ES6 tutorial.