Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

determining $(this) in jquery

Tags:

jquery

this

A common issue I have is getting confused what $(this) is referring to.

I often will try to give it some odd style:

$(this).css("border","10px solid red")

Which helps sometimes.

I'm stumped with the following however. My question can maybe be answered in two ways:

1) is there a universal way to 'see' what $(this) is referring to in any given situation? Perhaps in combination with firebug?

2) more specifically, any idea what $(this) should be referring to in this sample below? I assumed it would have been the input with a class of btnSave but doesn't seem to be:

$(ajaxContainer).find("input.btnSave").click(function(){
    savePanel();
});

function savePanel() {
    $(this).css("border","10px solid red");
};
like image 376
DA. Avatar asked Oct 27 '09 22:10

DA.


People also ask

What is jQuery $( this?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.

What does $() stand for in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.

How do you check if the ID exists in jQuery?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements. To check if an element which has an id of “div1” exists.

What does $() mean in javascript?

Usually when you encounter $() , that means the developer is using a javascript library, such as jQuery. The $ symbol is the namespace for those libraries. All the functions they define begin with $. , such as $. get() .


2 Answers

1) Use the console in Firebug:

var obj = $(this);
console.log(obj);

2) savePanel() won't have the context correct to use $(this). You could try:

$(ajaxContainer).find("input.btnSave").click(function(){
    $(this).css("border", "10px solid red");
});
like image 171
richsage Avatar answered Sep 23 '22 19:09

richsage


First question (using Firebug):

  1. Place a breakpoint somewhere in the context you want to investigate (e.g. inside savePanel() in your example).
  2. When your application hits the breakpoint, add $(this) to the watch panel and expand it to view its properties.
  3. The first property should be "0", which corresponds to the first DOM node matched by the jQuery object (in this case, this).
  4. If you hover over the value for "0", Firebug will highlight that DOM node on the page. If you click the value, Firebug will switch to the HTML tab and scroll to that element.

Second question:

  • Inside of your anonymous function, this will refer to the <input /> element.
  • Inside of savePanel(), this will refer to the window object.

If you want savePanel() to have access to the <input /> element, there are a variety of ways to do it. The simplest in your case would be to pass it in from the anonymous function:

$(ajaxContainer).find("input.btnSave").click(function(){
  savePanel($(this));
});

function savePanel(inputElement) {
  inputElement.css("border","10px solid red");
}
like image 28
Annabelle Avatar answered Sep 23 '22 19:09

Annabelle