0
the fourth response of the module 4 quiz (manipulate DOM) is incorrect in jquery tutorial course
following the jquery tutorial course i found that the fourth response of module 4 quiz gave me "2". I choosed "1" as response receiving an error. I have deepened the matter using the code playground environment and it gave me the same response i did. Could someone verify what i realized. I make public the code i used (https://code.sololearn.com/WBezfCPh71Px/#jquery). Thanks
3 Answers
+ 5
I've rearranged so I can talk about lines:
1: <div>
2: <p>1</p></div>
3: <div>2</div>
4: alert(
5: $("p")
6: .parent()
7: .siblings()
8: .eq(0)
9: .text());
Line 5 returns a pointer to the DOM of the paragraph of line 2. Line 6 takes that and gets it's parent (the division at line 1). Line 7 gets a list of all siblings of that division (there is only the division at line 3). Line 8 gets the pointer to the first silbling. Line 9 gets the text that that division contains (the 2.) Finally, line 4 has a string to output.
+ 3
That is because your code isn't the same. In the quiz, the alert is in a script within the body so it runs after the body is loaded. In your code, it is run before the body is loaded so there isn't any paragraph to find. If you add:
window.onload = function () {
alert(....) //existing code
}
your code will work correctly.
+ 1
Many thanks for your explanation it is important going on without doubts!