1

I'm attempting to create a DOM element using jQuery. I would like to create this DOM element with an 'on input' event defined.

This works:

var headerInput = $('<input/>', {
    'class': 'headerInput',
    type: 'text'
});

headerInput.on('input', function() {
    backgroundManager.get('activePlaylist').set('title', $(this).val());
});

This does not:

var headerInput = $('<input/>', {
    'class': 'headerInput',
    type: 'text',
    input: function(){
        backgroundManager.get('activePlaylist').set('title', $(this).val());
    }
});

I was wondering why? I thought these two syntaxes were identical.

1 Answer 1

2

It says:

As of jQuery 1.8, any jQuery instance method (a method of jQuery.fn) can be used as a property of the object passed to the second parameter.

but there is no such jQuery method input. Not for every event does an instance method exist.

You can use the on property as follows though:

var headerInput = $('<input/>', {
    'class': 'headerInput',
    type: 'text',
    on: {
        input: function(){
            backgroundManager.get('activePlaylist').set('title', $(this).val());
        }
    }
});

More info and examples can be found in the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh this is badass. I had no idea you could define on events like that. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.