Skip to content Skip to sidebar Skip to footer

Button Element Onclick To Run Javascript Function Without JQuery

Why won't this give an alert? I need to make it work without JQuery and with the 'button' element since the button is being auto-generated. It works if I put the javascript insid

Solution 1:


Solution 2:

Seems to work right for me here.

function test()
{
alert('hi');
}
<button onclick='test()' >click</button>

Solution 3:

Your function is not working correctly because, script is getting loaded at top of the html page. It should be loaded at the bottom if you want to access the DOM element

  • Click on the Javascript button
  • Select No wrap in body
  • It should work properly now

To rectify this.

Javascript Configuration


Solution 4:

The function test does not work in the jsFiddle environment because what you write in the javascript section is placed into the window.onload handler by jsFiddle.

If you do not want to change any settings, directly add to the global scope like here: https://jsfiddle.net/93a8rtaf/

this.test = function () {
  alert('hi');
}
<button onclick='test()' >click</button>

Post a Comment for "Button Element Onclick To Run Javascript Function Without JQuery"