While I am trying to learn jquery I learned that $(selector) returns an object which has all the match of that selector and is iterable like arrays. eg $("button") will return an object that will have access to all the button tag of DOM in such way that to access the first button tag you can use $["button"][0] for second you can use $["button"][1] and so on.
So here below is code focus on commented line 1 and line 2.
<body>
<button>Click me</button>
<script>
$(document).ready(function() {
// line 1
$("button").click(function() {
console.log("1");
// line 2
$("button").click();
});
});
</script>
</body>
line 2 inside line1 event handler function is set up an infinite loop as you can see that when I Click on "Click me" button it will trigger line1 inside of which is line 2 which too will again trigger line 1 and so on. now see the below code snippet with changed line2.
<script>
$(document).ready(function() {
// line 1
$("button").click(function() {
console.log("1");
// line 2
$("button")[0].click();
});
});
</script>
This time it is not setting up an infinite loop but prints to console "1" only two times why so?