+ 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?
16 Respostas
+ 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();
};
+ 8
😎
+ 6
+ 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 😎
+ 5
@Very Hard huh??
+ 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"
+ 4
@Daniele
just copy my Jquery code 😌
+ 3
ahhh.. do you copy my code ? its like same 😓
+ 3
LooL At least he save it as private
+ 3
Hey>>Huy>>Kuy
+ 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!
+ 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..😂
+ 1
@ArtemisAthena456
+ 1
and what should i do to make the code working?
+ 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 :)
0
Hey