Daniel Serretti
Daniel Serretti

Reputation: 342

Compare DOM Elements using jQuery

My task is very simple. I just want to compare two DOM Elements, like this:

HTML:

Are you a : Client <input id="client" name="cli_employ" type="radio" />
            Employer <input id="employ" name="cli_employ" type="radio" />

And my comparsion with jQuery:

$("input[name=cli_employ]").on("click", function() {
        // I'm sure my selector is working
        if ($(this) == $("#client")) { // How to compare?
            // do something
        }
        if ($(this) == $("#employ")) { // How to compare?
            // do something
        }

How can I compare those elements with the 'if' command? I just tried .is() and it always returns false. Thanks in advance.

Upvotes: 0

Views: 49

Answers (3)

Satpal
Satpal

Reputation: 133403

You can use .is()

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

Code example

if($(this).is("#client")){
   //Do something
}

Upvotes: 0

Simon
Simon

Reputation: 219

You should write:

if ($(this).attr('id') == 'client'))

Regards.

Upvotes: 1

M&#225;rcio Gonzalez
M&#225;rcio Gonzalez

Reputation: 1040

Did you try this way?

$("input[name=cli_employ]").on("click", function() {
    // I'm sure my selector is working
    if ($(this).attr("id") == "client") { // How to compare?
        // do something
    }
    if ($(this).attr("id") == "employ") { // How to compare?
        // do something
    }

Upvotes: 1

Related Questions