0

What is this jQuery code doing? --> $("div").text()

This is from the jQuery Module 5 quiz. The question asks what the output is after two clicks, which the answer is 111 and which I understand is the concatenation of strings of 1. I know that $("div").text()+1 is converting the 1 in the div to string 1 and concatenating to 1, so the output is "11" for this section. My question is why is the first $("div").text needed? Here is the quiz question below: What will be the output after the div is clicked two times? <div>1</div> <script> $("div").click(function() { $("div").text($("div").text()+1); }); </script>

8th Sep 2020, 12:56 AM
DMo
2 odpowiedzi
+ 3
It will add 1 to text of div and then replace the original text with the new text which is originaltext1 here $("div").text() is used to fetch the text inside div tag
8th Sep 2020, 1:40 AM
Ayush Kumar
Ayush Kumar - avatar
+ 2
$("div").text() us used here to get the text inside div In first time it will return 1 which concat with 1 to set text inside div 11. Second time it return 11 which concat with 1 to set div text 111. selector.text(); Is use to get the value of text inside selected element Selector.text("xyz"); Is used to set the value xyz in selected element.
8th Sep 2020, 2:32 AM
Divya Mohan
Divya Mohan - avatar