Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  98] [ 4]  / answers: 1 / hits: 33690  / 10 Years ago, sun, february 23, 2014, 12:00:00

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.


More From » html

 Answers
3

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.


[#72362] Friday, February 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
xochitl

Total Points: 559
Total Questions: 95
Total Answers: 117

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
;