6

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?

1
  • 2
    Every post using .innerHTML has it spelled wrong. I fixed one then realised you have all copy pasted the same error. Really should be more careful. Scripting languages usually react quite badly to the little errors it can take days to find ;-) Commented Sep 25, 2011 at 20:39

7 Answers 7

6

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Assuming you realy want to define a new funciton on your onclick event you can try this:

<input type="button" value="something" onclick="window.hello=function(){window.print();var z =document.GetElementById('ogog');z.innerHTML='hello';}">

Comments

4

You might want to try:

<script type="text/javascript">
    function hello() {
        window.print();

        var z =document.GetElementById('ogog');

        z.inneHTML="hello";
    }
</script>

<input type="button" value="sth" onclick="hello()"/>

3 Comments

And it will work on other site which is generated from the first?
Kooilnc: Indeed, force of habit :)
In that context javascript: is a label (e.g. for a loop). developer.mozilla.org/en/JavaScript/Reference/Statements/Label
3

You don't have to open the script tags, you just need to write directly the code you need.
You'd better include it inside a function defined in the head section, though:

<head>
<script type="text/javascript">
function hello () { ... }
</script>
</head>
<body>
<input type="button" onclick="hello()" />
</body>

or, even better, load it from an external .js file:

<script type="text/javascript" src="scripts.js"></script>

3 Comments

but if I do that it wont recognize that function on next generated page.And will do nothing
if you define that function in an external file, it will remain the same across the site. Otherwise, I'm not getting what you mean by 'recognizing'
I have to write a code which generate a new site it is in javascript.Now i want to have a button on my new generated site which call function.but I can not define function by generating them like html code I have to anchor it to that button.I also tried to call it from my old site but i think it is impossible, It is clearer now?
2

You will have to define the script tag in the same file first, also '{' for function definitions -

<script type="text/javascript">
function hello() {
window.print();
var z =document.GetElementById('ogog');
z.inneHTML="hello";
}
</script>

Then call the function with its name -

<input type="button" value="sth" onclick="hello()"/>

Check this link for more details.

Comments

1

You can also put code directly, as for example a jQuery call, as for example onclick="$('#confirmation_modal').modal();" to open a modal or onclick="$('#confirmation_modal').modal('toggle');" to close it.

Or also you can define a function and call it after.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.