I'm trying to make a jquery button click to change all text to a specific text in the parent div but it seems that I can't access variables that are outside of the jquery function
I tried many stackoverflow solutions but none of them worked and most of them looked very similar to my code so I don't understand why it didn't work.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
</head>
<body>
<div id="test">
<p>Click the button to get the tag names of the body element's children.</p>
</div>
<button>Try it</button>
<p id="demo"></p>
<script>
$(document).ready(function() {
var Dropdownchildren = document.getElementById("test").children
for (i = 0; i < Dropdownchildren.length; i++) {
console.log(Dropdownchildren[i].innerHTML)
// will work
$(Dropdownchildren[i]).click(function() {
console.log(Dropdownchildren[i].innerHTML)
// not work
})
}
})
</script>
</body>
</html>
I expected it to print the text but it didn't and even with global variables it couldn't access the variable and give me null or undefined.