Suppose I have an <input> inside a <button> like the following. Doing stopPropagation inside onclick stops a click on the text input from actuating the containing button. But when I press the spacebar while inside that text input, how do I stop that event from propogating and thus actuating the button?
In other words, I want to type space characters into the text input below without freely without getting an alert. Can this be done?
I thought the answer might be to make onkeyup or oninput stop propagation, but doing so doesn't change anything.
<button onclick="alert('button activated');">
Part of label
<input type="text"
onclick="(arguments[0] || window.event).stopPropagation();">
End of label
</button>
CodePudding user response:
You can add onkeyup with preventDefault to prevent key release of space which triggers that button click.
<button onclick="alert('button activated');">
Part of label
<input type="text"
onkeyup="(arguments[0] || window.event).preventDefault();"
onclick="(arguments[0] || window.event).stopPropagation();">
End of label
</button>
