Home / the Internet / Web programming / How to insert a code on a button

How to insert a code into a button


How to insert a code into a button</a>

Buttons in web pages are used to organize interactive interaction with the user.

Typically, if the response to a button does not require sending data to the server, the interactive is implemented using JavaScript scripts.

Ways to call the appropriate JavaScript code can vary - several possible options for buttons of different types are described below.



If the button is displayed in a web pageOrganized using the button tag, then the JavaScript code can be placed in the onclick attribute. For example, this is the case: & lt-button onclick = "alert ('Button is pressed!') - & gt-button & lt- / button & gt-Of course, putting a sufficiently large code directly in the button tag is not advisable - it's better to format it as a function, but to the onclick attribute Place only the call code for this function. For example: & lt-script & gt-
Function showAlert () {
Alert ('Button is pressed!')
& Lt- / script & gt- & lt-button onclick = "showAlert ()" & gt-button & lt- / button & gt-


If the button is displayed by one of theOptions for the input (submit, reset, button, or image) tag, you can use the same onclick attribute. For example, for the reset button, the code might look like this: & lt-input type = "RESET" onclick = "showAlert ()" / & gt- If you want the JavaScript script to be executed at the click of a button, and the assigned to it By default the action did not occur, then the return false command should be added to the function or directly to the onclick attribute. For example: & lt-input type = "RESET" onclick = "showAlert () - RETURN FALSE" / & gt-


If you want to organize a response to a button clickType submit, then, in addition to the above method using the onclick attribute, you can use the properties of the form tag to which this button belongs. The corresponding function call can be placed in the onsubmit attribute of the form tag. For example: & lt-form action = »» onsubmit = "showAlert () - RETURN FALSE" & gt-
& Lt-input type = "submit" / & gt-
& Lt- / form & gt-


If the button is not a form element, but the wholeOnly a graphic element (img tag), then for him the standards allow the use of the onclick attribute. For example: & lt-img src = "btn.gif" onclick = "showAlert () -" / & gt-


If the button is a hyperlink, thenUse the attributes of the button itself is not worth it, it's better to use the properties of the link tag. You can, as in the previous versions, use the onclick tag. For example: & lt-a href = "http://learn-myself.com" onclick = "showAlert () - RETURN FALSE -" & gt- & lt-img src = "btn.gif" / & gt- & lt- / a & gt-A Can be replaced by calling the function address in the href attribute. For example, this: & lt-a href = "javascript: showAlert () -" & gt- & lt-img src = "btn.gif" / & gt- & lt- / a & gt-

How to insert a code into a button Was last modified: June 20th, 2017 By Nolseowb
It is main inner container footer text