Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement, $(this) == $("#div")

Hya,

I would like to run a bit of code only if the focused text-box has a certain ID. Here is my code:

$("input:text").focus(function() {
        if ($(this) == $("#divid")) {
                //action
        }
});

It does not work, I'm not to sure why.

like image 724
Alexandre Hitchcox Avatar asked Nov 30 '25 05:11

Alexandre Hitchcox


2 Answers

Try comparing by the id directly.

if (this.id === "divid") {
    // do something
}
like image 196
Kevin B Avatar answered Dec 02 '25 19:12

Kevin B


That'll never work because each call to $() returns a new object.

However:

this == $('#divid')[0]

should work. Or, as Kevin B wisely suggests, just see if your element has the "id" in question.

like image 38
Pointy Avatar answered Dec 02 '25 17:12

Pointy