JQuery selector, To-do list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

JQuery selector, To-do list

https://code.sololearn.com/WgblwSDlA3qk/?ref=app this is a copy of an example code in jquery tutorial into this code there is a function which is into another function, but in my opinion they are not directly related to each other, so i wrote the inner function out of the other function, but the code doesn't work can someone explain me why these functions need to be one within the other to run correctly?

31st Jul 2017, 4:07 PM
Daniele
Daniele - avatar
16 Answers
+ 5
Your attempt to move the function outside will fail because you try to move out more than just a fuction declaration, but a call to a JQuery function with a function as parameter ^^ Let say, if you write: alert('hello'); ... you call a function named 'alert' with a string as argument to be displayed. Now, you can also do: function sentence() { return 'hello'; } function show(func) { alert(func()); } show(sentence); ... the 'alert' call use the function passed as argument of 'show'. In your case, the '
#x27; JQuery root object/function is called with a function declared inline... with the previous example, that will be: show(function() { return 'hello'; }); ... so you need to move out only the function declaration, but not the JQuery '
#x27; methods calls: $(".rem").on("click", function() { $(this).parent().remove(); }); will be: // not moved $(".rem").on("click", myfunctionname); // moved (but need to be in an accessible variable scope for the previous call ^^ function myfunctionname() { $(this).parent().remove(); };
2nd Aug 2017, 1:08 AM
visph
visph - avatar
+ 8
😎
1st Aug 2017, 1:34 PM
Art456
Art456 - avatar
1st Aug 2017, 12:41 PM
Art456
Art456 - avatar
+ 5
in here var elem = $("<li></li>").text(val); $(elem).append("<button class='rem'>X</button>"); $("#mylist").append(elem); $("input").val(""); $(".rem").on("click", function() { $(this).parent().remove(); and $(this).parent().remove(); make it remove and you see on $(".rem").on("click", function() { this means if .rem (the tag that have .rem) clicked it will run this $(this).parent().remove(); statement 😎
1st Aug 2017, 1:11 PM
Art456
Art456 - avatar
+ 5
@Very Hard huh??
1st Aug 2017, 1:15 PM
Art456
Art456 - avatar
+ 4
Bc you didn't add event to .rem when .rem append out @ArtemisAthena456 He want to learnnnnnnnn and understand and also he typed "in jQuery tutorial"
1st Aug 2017, 1:00 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
@Daniele just copy my Jquery code 😌
1st Aug 2017, 1:16 PM
Art456
Art456 - avatar
+ 3
ahhh.. do you copy my code ? its like same 😓
1st Aug 2017, 12:42 PM
Art456
Art456 - avatar
+ 3
LooL At least he save it as private
1st Aug 2017, 1:20 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
Hey>>Huy>>Kuy
1st Aug 2017, 2:20 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
thank you very much @visph for this example! it's a bit difficult for me to understand these part, since it's the first time i have met this kind of problem, but thank you for hepling me!
2nd Aug 2017, 5:16 AM
Daniele
Daniele - avatar
+ 1
maybe you copied mine😉😂 yes but i don't understand why the function which allow you to destroy elements you added before need to be inside the function which allow you to create those list elements..😂
1st Aug 2017, 12:57 PM
Daniele
Daniele - avatar
+ 1
@ArtemisAthena456
1st Aug 2017, 12:58 PM
Daniele
Daniele - avatar
+ 1
and what should i do to make the code working?
1st Aug 2017, 1:05 PM
Daniele
Daniele - avatar
+ 1
@ArtemisAthena456 yes i know your code works and also mine worked before i changed that part hahah, i only didn't understand why it didn't work if i put the function outside, i had only this doubt thank you both @ArtemisAthena456 and @Very_Hard for replying my question :)
1st Aug 2017, 1:21 PM
Daniele
Daniele - avatar
0
Hey
1st Aug 2017, 2:19 PM
Jawad El Ansari
Jawad El Ansari - avatar