Ask coding questions

← Back to all posts
Button to alert
indigiz (8)

Please provide code so that I can make a button that if you click it, it will show an alert. I only know the code for the alert: 'alert('EXAMPLE')'.

Answered by CodeMaster007 (109) [earned 5 cycles]
View Answer
CodeMaster007 (109)

So first make another file named "app.js". This will be the command or what will appear when you click the button. Ok, so now lets make the code! First lets write the index.js (or the main code).

<button class="btn btn-success">Install</button>
$('button').click(function () {
$.post('/page', {data: 'blah'}, function (data) {
}, 'json');

Ok! Lets do the app.js now! (this is the new file created).'/page', function (req, res) {
res.send('A message!');

And you got your code!!! In the browser console, you should see the command "a message" pop up. If you would like to change this, you can put whatever in instead of "a message!" for res.send('A message!'); like perhaps res.send('Alert!');, which is what I think you were going for.



JBloves27 (1878)

That is one part of it, but here would be example code:


<script src="index.js"></script>
<button onclick="myFunction()"A button!</button>


function myFunction() {
  alert("Hello World!");

That should work! (as an example)