highlander141
highlander141

Reputation: 1725

Using two functions in JavaScript

I would like to add two functions in javascript such that after first one validates, the second must verify and return the value. But only one is executing in my case:

Here is the code:

<html>
<script type="text/javascript">
function validate(){
var name_value=document.getElementById('qs').value;
if(name_value=="")
{
alert("Please enter your keyword");
}
}
var ray={ajax:function(st)
    {
        this.show('load');
    },
show:function(el)
    {
        this.getID(el).style.display='';
    },
getID:function(el)
    {
        return document.getElementById(el);
    }
}
</script>
<style type="text/css">
#load {
    background: none repeat scroll 0 0 #F7F7F7;
    border: 1px double #999999;
    font-family: "Trebuchet MS",verdana,arial,tahoma;
    height: 50px;
    left: 45%;
    line-height: 50px;
    margin-left: -150px;
    margin-top: -50px;
    position: absolute;
    text-align: center;
    top: 50%;
    width: 350px;
    z-index: 1;
}
</style>
<body>
<div id="load" style="display:none;"><strong>Please wait while we process your request...</strong></div>
<form action="http://www.example.com" method="post" onSubmit="validate() && return ray.ajax()">
<input type="text" value="" name="q" id="qs">
<input type="submit" value="Search">
</form>
</body>
</html>

In the above code, only validate() will work if I remove return ray.ajax() in the <form> tag. But I want both to work i.e. if the textbox isEmpty, show alert, else show Please wait... message.

How to achieve it? Thanks in advance...

Upvotes: 0

Views: 178

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

function validate(){
    var name_value=document.getElementById('qs').value;

    if(name_value=="")
    {
        alert("Please enter your keyword");
        return false;
    }
    return true;
}
var ray={
    ajax:function(st)
    {
        this.show('load');
        return true
    },
    show:function(el)
    {
        this.getID(el).style.display='';
    },
    getID:function(el)
    {
        return document.getElementById(el);
    }
}

and

<form action="http://www.example.com" method="post" onSubmit="return validate() && ray.ajax()">

Upvotes: 1

Related Questions