<a href="#" type="image" class="topopup" onclick="ShowDIV3();" >Click Here</a>
In this code I need to execute another one function like validation if that function is true only the next function should run otherwise it won't be run. Can any one help me?
Try this code :
Click Here
Try like
<a href="#" type="image" class="topopup" onclick="ShowDIV3();" >Click Here</a>
function ShowDIV3() {
if(true) //Here can use condition for validation
another_fun();
else
return false;
}
call your another function when the condition is true at your showDIV3 function
Try this way
onclick=" if (ValidateFunction()) return ShowDIV3();"
in jquery you could do it like this, this will also allow multiple handlers and better event normalization and improved separation of concerns.
$(".topopup").on("click", function(){
if (ShowDIV3()){
OtherStuff();
}
});
In your showDIV3() function at the end have something like this,
function showDIV3()
{
// Your process, and then at the end,
if(result)
{
// Call the other function
}
else
{
// Return false
}
}
Why returning there and executing ?? why not simply ??
function ShowDIV3(){
-----
if(resultBoolean){
proceedToanotherFunc();
}
}
Imho This would be more readable,other that writing logic there in html
Do your validation in ShowDIV3() function, and depending on the validation call your next function from inside the ShowDIV3() function
Just create more statement in your javascript like
function ValidateStatments() {
var val = document.getElementById("Value 1").value;
var val2 = document.getElementById("Value 2").value;
if(val != "Value 1"){
return false;
}
else if (val2 != "Value2){
return false;
}
return true;
}
and on your link add your javascript so if its true, it will allow the user to do the action onclick="return ValidateStatments();"
Related
I have a jQuery event inside a JavaScript function. I've already read that you cannot access the inner function. However, I would like to know how to adjust my code so that the parent function returns true or false depending on the jQuery function.
function validate() {
$("#button").on('click', function(){
var input = document.forms["formular"]["text"].value;
if (input == "") {
return false;
}
});
if(onclickfunction() == true){
return true;
}
else{
return false
}
}
validate();
Or can you recommend a different approach?
Not sure what this code is supposed to do, because calling validate only creates the event listener without actually executing it. But what you can do is to prevent the default action when you need, which is how validation is usually implemented:
$("#button").on('click', function(){
var input = document.forms["formular"]["text"].value;
yourSecondFunction(input !== "");
});
function yourSecondFunction(inputIsValid) {
// Do your magic here
}
Arter clicking i need to run chkMethode() in javaScript and then I decide is it go to the url or not
When i clicked 'Go' it does not run chkMethode() directly go to the given link. what to do?
Go
It's because JavaScript needs to prevent the anchor from performing its usual event and it does this using preventDefault. Here, as best practice, I've separated out your JS from the HTML and used an id.
HTML
<a id="go" href="http://www.google.com">Go</a>
JS
var go = document.getElementById('go');
go.onclick = chkMethode;
function chkMethode(e) {
e.preventDefault();
if (check) {
window.location.href = this.getAttribute('href');
}
}
function chkMethode(event)
{
if (<<certain condition>>)
{
event.preventDefault();
return false;
}
}
Go
<a onclick="event.preventDefault();chkMethode();" href="http://www.google.com" > Go </a>
Function chkMethode () {
Var valuetochck = // value to be checked
If(valuetochck == "")
{
Window.location.href="url1";
}
Else
{
Window.location.href="url2";
}
}
At the bottom of my webpage, I have a submit button. I've used this code:
<a onClick="myFunction(); return false;" href="testpage.html">Submit</a>
What I'm trying to do is when my function is called, I'm checking for validation. If it's false, my function raises an alert and the user doesn't leave the current page...which is what I want. When its true though...nothing changes. When it's true I want them to go to the next link.
function myFunction() {
if (localStorage.length != 3) {
alert("Missing Values");
} else {
break;
}
}
It goes to the next link when I put in break, but now the alert doesn't get called even if it's requirements are met. Moreover, whey does the break in the else block get called even when the if block requirements are met?
Well return false cancels the action. So if you do not want to stop the link, you need to remove that.
<a onclick="return myFunction();" href="testpage.html">Submit</a>
Now return true or false from myFunction
function myFunction() {
if (localStorage.length != 3) {
alert("Missing Values");
return false;
}
return true;
}
Rewrite your html to <a onClick="myFunction()" href="testpage.html">Submit</a> and function to:
function myFunction() {
if (localStorage.length != 3) {
event.preventDefault();
}
}
See Fiddle: https://jsfiddle.net/ermakovnikolay/L0q7ocgg/
I have 3 javascript functions:
validateClk(), validateAmPm() and getClks()
And on two occurring events, they get executed as follows:
OnChange - executes validateClk() and validateAmPm()
OnClick - executes getClks() (getClks() returns a boolean value)
All 3 functions run correctly, but the problem is, after getClks() has finished execution and returns a boolean, the next function postClocks() doesn't run. I'm very sure that the code for postClocks() is correct as well. If I don't use the return statement for getClks() then the getClks() function doesn't work as expected.
Please help :(
<script type='text/javascript'>
function validateClk() {
....
var clks = clocks.value;
if (clks == "") {
alert('Enter time');
}
else { ... }
}
</script>
<script type='...'>
function validateAMPM() {
...
var ampm= ap.value;
if (ampm=="") {
alert('Enter am or pm');
}
}
</script>
<script type='text/...'>
function getClks() {
var clks= clock.value;
var ampm= ap.value;
if (clks==" && ampm="") {
alert('Enter time and am/pm');
return false;
}
else { ... }
return true;
}
</script>
<... onChange="validateClk(); validateAmPm();" />
<... button label="Submit" onClick="return getClks(); postClocks(); return false;" />
It's because you explicitly coded a return in there.
return getClks();
postClocks();
return false;
That code will always just exit after that first return statement. I suggest removing it.
have all of your custom functions return boolean then change the onclick event to this:
onClick="if(!getClks() || !postClocks()) return false;"
assuming you don't want to continue if invalid
You wrote
onClick="return getClks(); postClocks(); return false;"
You have to remove the first "return".
Is there any way to do the following:
validateLogin();
return false;
But actually like this..
validateLogin();
And here is the function:
function validateLogin(){
if(hi=true){
return true;
}
else{
return false
}
I want to attach this function for the event of a form being submitted, so if HI is false - i want to return false; meaning it will stop for form from submitting.. i know return false; will do so i just need to know how i can get this return back to the parent function?
Thanks in advance!
You can use it as follow:
return validateLogin();
however, as mmayo pointed out, don't forget about the return value:
event.returnValue = false;
You can eventually do that:
return validateLogin();
Note that your function code has some errors (maybe due to the simplification of the code you made to post this question?). You'd better write this method like that:
function validateLogin(){
...
return hi;
}
Note also that insted of having if (hi=true) {, you must write if (hi == true) {, or better if (hi) {...
I use the following to stop events...
event.returnValue = false;
cresentfresh inspired me to do some research... here is an article with a compatibility matrix.
There are also related threads on SO.
return validateLogin();
This should do what you want.
The standard way of stoping the default action of an event is:
event. preventDefault();
You may also want to prevent event propagation with event.stopPropgation(); to stop further event listeners from executing.
http://www.w3.org/TR/2001/WD-DOM-Level-3-Events-20010823/events.html#Events-Event
However, IE will not recognize this which is why you can set event.returnValue to false for IE.
eg:
if (event && event.preventDefault) event.preventDefault();
event.returnValue = false;
You can also return false from the event handler for events inlined in HTML.
<form onsubmit="return validateLogin()">
This is not considered best practice however.
Note: the event object is passed in as the first argument in your event listener.
eg:
function validateLogin(e) {
e; // is the event object
}
For IE you may need window.event.
function validateLogin(e) {
e = e || window.event; // is the event object
}
Try double == (IF I==5)
validateLogin() Function
function validateLogin() {
return hi;
}
HTML block
<form onsubmit="return validateLogin()">
...
</form>