This may be a basic question. I have a button which is
<button type="button">Click Me!</button>
And then I have a script which is:
<script>
alert("My First JavaScript");
</script>
To call this script I can say onclick call another php or html file. But I want to add this script to the same file instead of adding a new file. Any suggestion will be appreciated.
Couple of ways:
1st
<button type="button" onclick="clickHandler()">Click Me!</button>
<script>
function clickHandler() {
alert("something");
}
</script>
2nd (if you are using something like jQuery)
<button id="btn" type="button">Click Me!</button>
$('#btn').click(function() {
alert('something')//
});
you may also do this in plain javascript.. just search for add event handler and you will get plenty of cross browser ways of doing this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With