Hi How can I define a function on button click in HTML, I tried something like that:
<input type="button" value="sth" onclick="
<script type="text/javascript">
function hello()(
window.print();
var z =document.GetElementById('ogog');
z.inneHTML="hello";
<script>
">
I have to define my function here because I have code which generate me a site in which's header i can not define javascript function and I want one function on button click. this is only an example. This is the way it should look like?
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.
Just invoke the code you're trying to invoke: onclick="alert('hello')" If you want to define a function, do that separately in your JavaScript code and just invoke the function in onclick . (Or, even better, attach it as a handler from the JavaScript code so you're not writing in-line JavaScript in your HTML.)
You can attach a script to a button by putting a string representation of that script in the onclick handler:
<input type="button" onclick="window.alert('Hi!')">
This code will be executed when the button is clicked. In general, the script tag is used to define scripts that can be referenced elsewhere in the HTML document, while strings containing JavaScript code are used in the body of the document to actually execute that code.
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