ditto
ditto

Reputation: 6297

On keyup or click get this element

I want to know which element has been clicked or keyuped. Something like the below:

$('button, input').on('click keyup', function() {

    var self = $(this);

    if (self == button)
        ...
    else if (self == input)
        ...

};

I can't do self.attr('name') == 'something' because the button has no name attribute.

Upvotes: 0

Views: 250

Answers (2)

Popnoodles
Popnoodles

Reputation: 28419

if ($(this).is('button')) {
} else if ($(this).is('input')) {
}

What malificent meant was

if ($(this).prop('tagName')=='BUTTON'){
} else if ($(this).prop('tagName')=='INPUT') {
}

though I would use a switch because it's tidier

switch ($(this).prop('tagName')){
    case 'BUTTON':
        //do stuff
    break;
    case 'INPUT':
        //do stuff
    break;
}

Upvotes: 1

malificent
malificent

Reputation: 1441

if you want to distinguish if it's an input or button tag you can use

$(this).prop("tagName")

Upvotes: 0

Related Questions