views:

126

answers:

3

I am trying to call my function named isUrgencyTypeValid from my javascript code but it is not working. Please check what is the problem in my code. Alert should get displayed which is not being displayed

My javascript function is not being called.

HTML Code

       <td colspan="2" align="center"><input id="btnSubmit" type="submit" value="submit" runat="server"/></td></tr>

jQuery Call function

        $("#btnSubmit").bind("click",function(){
        alert(); // this is running
        isUrgencyTypeValid();

        });

javascript implemented function

function isUrgencyTypeValid()
    {
        alert("asd");
        var i=0;
        for(i=0;i<$("radio[name='urgencyType']").length;i++)
            {
            if($("radio[name='urgencyType']")[i].checked)
                {
                alert($("radio[name='urgencyType']")[i].value);                 
                return true;
            }           
        return false;
    }   

More description about my form is here

<form runat="server" name="myPage">
    <table style="width: 100%;" cellpadding="5" cellspacing="5">
        <caption>
            Computer Support / Service Request
        </caption>
        <tr><td>First Name</td>
            <td><input id="txtFirstName" type="text" value="First Name" runat="server"/><span class="error"></span></td></tr>

        <tr>
            <td>Last Name</td>
            <td><input id="txtLastName" type="text" value="Last Name" runat="server"/><span class="error"></span></td></tr>

        <tr>
            <td>Email Address</td>
            <td><input id="txtEmailAddress" type="text" value="Email Address" runat="server"/><span class="error"></span></td></tr>    

        <tr>
            <td>Phone No</td>
            <td><input id="txtPhoneNo" type="text" value="Phone No" runat="server" /><span class="error"></span></td></tr>

        <tr>
            <td>Do you have text messaging</td>
            <td>
                <span>Yes</span><input id="rdoYes" value="Yes" type="radio" runat="server"/>
                <span>No</span><input id="rdoNo" value="No" type="radio" runat="server"/><span class="error"></span>
            </td></tr>

        <tr>
            <td>Description of request*: </td>
            <td><textarea id="txtDescription" cols="50" rows="10" runat="server"></textarea><span class="error"></span><span id="lengthCount"></span></td></tr>

        <tr>
            <td>Urgency of this support request:</td>
            <td>
                <input id="rdoAnyTime" name="urgencyType" value="Anytime" type="radio" runat="server"/><span>Anytime</span><br />
                <input id="rdoCplDays" name="urgencyType" value="In the next couple of days" type="radio" runat="server"/><span>In the next couple of days</span><br />
                <input id="rdoToday" name="urgencyType" value="Today" type="radio" runat="server"/><span>Today</span><br />
                <input id="rdoUrgent" name="urgencyType" value="This is extremely urgent...I cannot wait!" type="radio" runat="server"/><span>This is extremely urgent...I cannot wait!</span><br />
                <input id="rdoTalkSometime" name="urgencyType" value="Please contact me and we'll talk about it" type="radio" runat="server"/><span>Please contact me and we'll talk about it</span><br /><span class="error"></span>
            </td></tr>

        <tr>
            <td colspan="2" align="center">Captcha To Be implemented.</td></tr>

        <tr>
            <td></td>
            <td><input id="chkRequestCopy" type="checkbox" runat="server"/>Please send me a copy of this service request</td></tr>

        <tr>
            <td colspan="2" align="center"><input id="btnSubmit" type="submit" value="submit" runat="server"/></td></tr>

    </table>    
</form>   
A: 

Your radio selector is incorrect.

There's not such thing as a radio element, but there is a :radio pseudo-class selector.

Your code should read:

function isUrgencyTypeValid()
{
    alert("asd");
    var i=0;
    for(i=0;i<$("input[name='urgencyType']:radio").length;i++)
        {
        if($("input[name='urgencyType']:radio")[i].checked)
            {
            alert($("input[name='urgencyType']:radio")[i].value);                 
            return true;
        }           
    return false;
}

** EDIT **

In addition to the changes above, since the alert never occurs, this indicates that isUrgencyTypeValid is not being called.

The most likely explanation is that you're not waiting until the DOM is loaded, before you bind the your handler to the click event of the button. You should have some code somewhere that looks like this:

$(function()
{
    /*
     * ... other code ...
     */

    $("#btnSubmit").bind("click",function(){
        isUrgencyTypeValid();
    });

    /*
     * ... other code ...
     */
}
Dancrumb
@Dancrumb, sry this code is also not working. At least alert should have popped up. But i think it is not even entering into the function. Control is reaching at jquery function where i called this function. But i dont know what is happening afterward.
Shantanu Gupta
@Shantanu, my answer about the incorrect selector still stands, but if the alert is not even showing... that's a different matter. When are you binding the function? Are you waiting until the DOM is loaded before doing your bind?
Dancrumb
@Dancrumb: "Are you waiting until the DOM is loaded before doing your bind" I dont know about it. But when i presses button. It should show me an alert message at jqery and one at javascript as both are being called at same event. Moreover javascript function is being called by jquery function itself i.e. on click event
Shantanu Gupta
A: 

Have you included the jquery library in your tags?

Put something like this at the top of your page:

 <script type="text/javascript" 
         src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;
 </script>   
John Mee
@John yes, I have written this code. <script src="http://code.jquery.com/jquery-latest.js"></script>
Shantanu Gupta
+1  A: 

I copied your code and ran into the same issue. Then I looked at it closer and noticed you are missing a closing bracket in your isUrgencyTypeValid() function. The closing bracket for your for loop is missing. Firefox Error Console will also show this.

Once I added the bracket the alert worked fine.

function isUrgencyTypeValid()
{
    alert("asd");
    var i=0;
    for(i=0;i<$("radio[name='urgencyType']").length;i++)
        {
        if($("radio[name='urgencyType']")[i].checked)
        {
            alert($("radio[name='urgencyType']")[i].value);                 
            return true;
        }  
    } //this was missing
    return false;
}   
Erikk Ross
what the blunder i was doing. I was searching for mistake for last 3 hours. That's really shamefull to me. Thx Erikk for making me this into notice. 1 more Question: Why this is affecting alert given in previous step. It should have got printed up as per my knowledge. Any reason ?
Shantanu Gupta
I think you are asking why the alert() from your button click was not working? The one defined in the bind event? Since you had that syntax error in the isUrgencyTypeValid() function, it was affecting all of your JavaScript code on the page from running properly. The Firefox Error console should be your new best friend. :-) Or if you are running Firebug and have it's console open it will show all errors as well.
Erikk Ross