translate jquery to js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

translate jquery to js

how could we write this in js ? <div id="div1"></div> <div id="div2"></div> <script> $("div").click(function(){ var a = $(this).attr("id"); }); </script>

10th Mar 2018, 9:05 AM
NoxFly
NoxFly - avatar
23 Answers
+ 8
ha c'est avec Angular... Une piste à peaufiner : Comme c'est Angular, tu vires "this.id" et le deuxième paramètre. Tu écris comme çà : "ng-click("removePoints($event)")". Après, tu change toute ta fonction comme çà : $scope.removePoint = function(e) { var plus = e.currentTarget.attributes.id.nodeValue; if(plus === "Strenghtmore") { if($scope.pointsLeft <= 20 && $scope.pointsLeft > 0 ) { $scope.pointsLeft--; } } else { if($scope.pointsLeft < 20 ) { $scope.pointsLeft++; } } } Tu auras bien les points qui ce vide jusqu'à 0 et qui ce remplissent jusqu'à 20 en détectant si tu clique sur plus ou moins. Après, il faudra faire des if pour cibler plus chaque partie pour ajouter les points au bon endroit.
10th Mar 2018, 4:02 PM
MecyDev
MecyDev - avatar
+ 12
Another simple solution. <div id="div1" onclick="recup(this)"></div> <div id="div2" onclick="recup(this)" ></div> <script> function recup(x) { var a = x.id; } </script>
10th Mar 2018, 10:08 AM
MecyDev
MecyDev - avatar
+ 12
We need "jQueriel" like Babel😂😂😂
10th Mar 2018, 11:20 AM
\__(° = °)__/
+ 12
You can use just sizzle module, or if I remember correctly can include only slim version of jQuery.
10th Mar 2018, 11:29 AM
\__(° = °)__/
+ 11
// read this (really helpful): // http://youmightnotneedjquery.com function click() { var a = this.getAttribute("id"); } [].forEach.call(document.getElementsByTagName("div"), function (div) { div.addEventListener("click", click, false); }); // without extra variable: [].forEach.call(document.getElementsByTagName("div"), function (div) { div.addEventListener("click", this, false); }, function () { var a = this.getAttribute("id"); });
10th Mar 2018, 9:32 AM
Vladislav Tikhiy (SILENT)
Vladislav Tikhiy (SILENT) - avatar
+ 11
Petite erreur de copie. Dans ng-click c'est "removePoint" sans le "s".
10th Mar 2018, 10:53 PM
MecyDev
MecyDev - avatar
+ 9
thanks Emile Djida and SILENT :)
10th Mar 2018, 11:28 AM
NoxFly
NoxFly - avatar
+ 8
De rien.;-)
10th Mar 2018, 4:07 PM
MecyDev
MecyDev - avatar
+ 7
Pourtant, ça doit fonctionner. La solution de @SILENT est la plus avancé. Celle de @Emile Djida est equivalente voir avec ajustement la plus rapide. Ma version est la plus rapide mais un peu chiante, car il faut toujours ajouter un onclick dans les balises.
10th Mar 2018, 12:00 PM
MecyDev
MecyDev - avatar
+ 7
@NoxFly Montre un exemple de ton code.
10th Mar 2018, 1:56 PM
MecyDev
MecyDev - avatar
+ 6
😂 4rontender
10th Mar 2018, 11:27 AM
NoxFly
NoxFly - avatar
+ 6
c.mechain, jai essayé mais ca marche pas :/ jdois mal m'y prendre
10th Mar 2018, 11:28 AM
NoxFly
NoxFly - avatar
+ 6
thanks Basile Laderchi
10th Mar 2018, 1:04 PM
NoxFly
NoxFly - avatar
+ 5
@SILENT , I agree totally 👍
10th Mar 2018, 10:56 AM
Morpheus
Morpheus - avatar
+ 5
it's not advisable to waste bandwidth on full jQuery when all u need is a little code, moreover jQuery should not be used by beginners as it ll hinder your learning process , but it's good to know it makes development easier
10th Mar 2018, 10:59 AM
Morpheus
Morpheus - avatar
+ 5
regarde dans le js jai ciblé la zone https://code.sololearn.com/WRuGuKbTvpmi/?ref=app
10th Mar 2018, 2:11 PM
NoxFly
NoxFly - avatar
+ 5
oki
10th Mar 2018, 10:55 PM
NoxFly
NoxFly - avatar
+ 4
nickel merci
10th Mar 2018, 4:05 PM
NoxFly
NoxFly - avatar
+ 3
var divs= document. getElementsByTagName("div"); for(var i=0; i<divs.length; i++){ divs[i].onclick= function(){ var a= this.getAttribute("id"); }; }
10th Mar 2018, 9:33 AM
JustCodeMore
JustCodeMore - avatar
+ 3
I found this website to be very useful: https://plainjs.com
10th Mar 2018, 10:34 AM
🤖 Basile Laderchi