Hello I’m a newcomer in JavaScript and JQuery language. I started to see some examples of JQuery script.
i have the following code segment:
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
My question: what is the meaning of word “this” in this line of code:
$(this).hide();
It refers to each of
ptags in your selector$("p")that gets clicked. For example, you can see html of eachptag you clicked on like this:Notice also that
$(this)andthisin above context mean different things. The latterthisrefers to DOM element itself which won’t have jQuery methods/properties available to it, for example:Won’t work because
html()won’t be available becausethisrefers to DOM element in there. So if you want to use jQuery methods, use$(this)instead.