0

I am writing a draggable html div as:

enter image description here

And it looks like this:

enter image description here

But I cannot input anything like a normal input div as: <input />. It does not response for any key events.

I have tried to use stopPropagation to stop the event to its parents as:

    input.onclick = function ( evt ) {
        evt.stopPropagation();
        console.log( 'input got clicked' );
    };


    $( input ).on( 'keydown', function ( evt ) {
        evt.stopPropagation();
        console.log( 'input got keydown' );
    } );

where the input is:

    let input = document.createElement( 'input' );
    input.setAttribute( 'type', 'text' );

and with console.log( input ):

enter image description here

but it does not help. (And for the later keydown event, no output is given in the console.)

Can anyone suggest me a way to debug this problem? It really drives me crazy. Thanks!

PS: Chrome is used.

Update: I find the problem, but do not know the reason.

It is because I decorate the parent dom as jquery draggable, and I need to cancel the <input class='x-leaf'/> as:

    $('#input-parent').draggable({
        containment: 'window',
        cancel: '.x-leaf'
    });

With the previous stopPropogation and the way suggested by @Brainfeeder, it finally works.

7
  • show us how you have defined input in $( input ) looks like. Commented Jan 24, 2018 at 13:46
  • 1
    $('input') <-- needs the quotes around "input" Commented Jan 24, 2018 at 13:47
  • @Cruiser correct, unless he has defined input as a variable, and if he has, it should still be done in another way Commented Jan 24, 2018 at 13:48
  • You must be getting an error, input in not defined Commented Jan 24, 2018 at 13:48
  • Thanks for the reply. input is a variable, and I have edited it in the question. Commented Jan 24, 2018 at 13:51

1 Answer 1

1

Because input is created after DOM loading you better call .on() on a parent element that exists in DOM on page load.

$('#someParentEl').on( 'keydown', '.x-node input', function ( evt ) {
    console.log( 'input got keydown' );
} );
Sign up to request clarification or add additional context in comments.

7 Comments

try changing $(input) to .x-node input or some selector matching the input instead of the object.
` $( document ).on( 'keydown', $( '.x-node input' ), function ( evt ) { evt.stopPropagation(); console.log( 'input got keydown' ); } );`
Thanks for your patience. I have detected the event with the above code. But I still cannot input anything.
The real weird thing is that, once I add this input.classList.add('x-connection');, then it works, even with my previous buggy code.
.x-connection{ position:absolute; border:solid 1px #dedede; background-color:#2e2e2e; width:0.5em; height:0.5em; border-radius:0.5em; }
|

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.