Skip to content
Advertisement

Can you pass console.log() as a javascript expression into a HTML element method?

I want to know if it’s possible to console log strings from a html element’s method?

For example in an html document with:

<body>
  <form action="#">
    <label for="name"></label>
    <input type="text" id="name" onblur={console.log("Foo")}>
  </form>
</body>

I was able to console log numbers but not strings or booleans. Is is possible?

Advertisement

Answer

Yes of course try this code. The onblur attribute fires the javascript code at the moment that the element loses focus

    <form action="#">
        <label for="name"></label>
        <input type="text" id="name" onblur="console.log('test')">
    </form>
Advertisement