开发者

trigger 2 functions on an onclick event

开发者 https://www.devze.com 2023-03-16 04:28 出处:网络
I am calling 2 functions on an onclick event of a button , first function to check the form validations and the next one for hiding the form if and only if the validation is true. But the problem here

I am calling 2 functions on an onclick event of a button , first function to check the form validations and the next one for hiding the form if and only if the validation is true. But the problem here is when i click the Button both the functions are been simultaneously calle开发者_StackOverflow社区d and eventhough validation is false form gets hided quickly. Any solution to this would be helpful . THANKS

       <p><input type="button" name="send" value="Hide Entries" onclick="validate(),hide();"></p>


call the hide function with in the validate function

function validate()
{
   //other validation code
   hide();
}


both the functions are been simultaneously called

No, they're not, JavaScript on browsers is single-threaded (barring the use of web workers). They're being called one after another, because you haven't done anything to tell the interpreter not to run the hide function if validate returns false.

The minimal change would be:

<p><input type="button" name="send" value="Hide Entries" onclick="if (validate()){hide();}"></p>

...if you want hide to only be called if validate returns a truthy value.


Off-topic (slightly): The larger change would be to move the JavaScript logic out of the HTML markup and into a proper script. Recommend reading up on unobtrusive JavaScript. But the above should work with your current structure.

0

精彩评论

暂无评论...
验证码 换一张
取 消