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
Comments
hotnewtop
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>
<script>
$('button').click(function () {
$.post('/page', {data: 'blah'}, function (data) {
console.log(data);
});
}, 'json');
</script>

Ok! Lets do the app.js now! (this is the new file created).

app.post('/page', function (req, res) {
calling.aFunction();
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.

Thanks,

CodeMaster007

JBloves27 (1878)

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

index.html

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

index.js

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

That should work! (as an example)