0

I have following setup in my Backbone View:

var SomeView;

SomeView = Backbone.View.extend({
    initialize: function() { this.render(); },
    render: function() { // render form },
    events: {
        "keydown form.registration input": "checkInput"
    },
    checkInput: function(e) {
        console.log(e);
        // this doesn't work but I am searching for such a function
        var attr = e.getAttributeWhichTriggeredEvent;
        $(attr).val();
        // validate...
    } 
});

As you see, I would like to get the selector of the element which triggered the event, so that I can, for example, use the input value.

As I looked through the console in Chromium, I found some (current) target attributes in the event object. Unfortunatly they don't contain anything which I could use to identicate the element but maybe I just not looked enough..

So how do I do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dev.pus
  • 7,919
  • 13
  • 37
  • 51

2 Answers2

1

I think you want..

$(e.target).val()
lecstor
  • 5,619
  • 21
  • 27
1

DOM Event objects have a type that reflects the type of event that dispatched them (e.g. click, mousedown, etc.). They also have properties for target and currentTarget that reference DOM elements related to the event, one of those may suit.

Note that older IE doesn't support event.target but has event.srcElement instead.

RobG
  • 142,382
  • 31
  • 172
  • 209