-1

I use firebug to see the following code,

<input type="text" oncut="return false">

then try to get the return value of the event oncut,

var a=document.querySelector('input[type="text"]');
document.write(a.oncut);

I want to get the return value of oncut: false, however , I get the definition of the function oncut,as follows,

function oncut(event){
return false;
}

How can I only get the return value "false" from the event oncut?

Matt Elson
  • 3,419
  • 8
  • 27
  • 41
  • if your returned value is constant, then just call the returned function. – amberv May 07 '16 at 10:00
  • You should explain why you want to have the return value. In this simple case you can just call the function. But a real callback will - in most cases - always use the input event or other data, so calling the function will often fail. – t.niese May 07 '16 at 10:04

2 Answers2

0

oncut in your case is intended as a handler(event handler). "return false" is a definition of that handler. To get the returned value from function - just call the function:

...
document.write(a.oncut());
...

For example:

window.onload = function(){
    document.write(document.getElementsByClassName("cut")[0].oncut());
}
<input type="text" class='cut' oncut="return false">
RomanPerekhrest
  • 73,078
  • 4
  • 37
  • 76
0

You can declare a function (giving it an arbitrary name) then add it to the cut event. Place your statement within this function. I used contentText instead of document.write because of this.

function cutFalse() {
  return document.body.textContent = false;
}
<input type="text" oncut="cutFalse()">
Community
  • 1
  • 1
zer00ne
  • 31,838
  • 5
  • 32
  • 53
  • While I agree that `document.write` should not be used, it might still be _valid_ for short testing (but i would still suggest to always use `console.log`), using `document.body.textContent = ....;` is nearly identical to `document.write`. – t.niese May 07 '16 at 10:27
  • In this situation of course the body is the target. I was tempted to add a div to receive the result, but it looked as if the OP objective was to have the result remain in the input's place. – zer00ne May 07 '16 at 10:30