Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Call a JS function using OnClick event

I am trying to call my JS function that I added in the header. Please find below code that shows my problem scenario. Note: I don't have access to the body in my application. Everytime I click on the element with id="Save" it only calls f1() but not fun(). How can I make it call even my fun()? Please help.

  <!DOCTYPE html>   <html>   <head>    <script>     document.getElementById("Save").onclick = function fun()     {      alert("hello");      //validation code to see State field is mandatory.       }         function f1()     {        alert("f1 called");        //form validation that recalls the page showing with supplied inputs.         }    </script>   </head>   <body>   <form name="form1" id="form1" method="post">             State:              <select id="state ID">                <option></option>                <option value="ap">ap</option>                <option value="bp">bp</option>             </select>    </form>     <table><tr><td id="Save" onclick="f1()">click</td></tr></table>     </body>    </html> 
like image 566
Vineeth Varma Avatar asked Jan 31 '14 10:01

Vineeth Varma


People also ask

How do you call a function onclick event in react JS?

The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app. In addition, React event handlers appear inside curly braces.

How Onclick works in JavaScript?

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 call a function on a div click?

<div class="test">Click me! </div> $('div. test'). click(function(){ GetContent("xxx"); });

How do you trigger a function in JavaScript?

onchange: It is triggered when an HTML element changes. onclick: It is triggered when an HTML element is clicked. onmouseover: It is triggered when the mouse is moved over a HTML element. onmouseout: It is triggered when the mouse is moved out of a HTML element.


1 Answers

You are attempting to attach an event listener function before the element is loaded. Place fun() inside an onload event listener function. Call f1() within this function, as the onclick attribute will be ignored.

function f1() {     alert("f1 called");     //form validation that recalls the page showing with supplied inputs.     } window.onload = function() {     document.getElementById("Save").onclick = function fun() {         alert("hello");         f1();         //validation code to see State field is mandatory.       } } 

JSFiddle

like image 153
George Avatar answered Sep 23 '22 04:09

George