Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define function in javascript onbuttonclick

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?

like image 203
Csabi Avatar asked Jan 31 '11 10:01

Csabi


People also ask

How do you define a function in JavaScript?

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).

How do you define a function onclick?

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.

How do you write a function on onclick event?

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.)


1 Answers

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.

like image 78
templatetypedef Avatar answered Oct 09 '22 16:10

templatetypedef