Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Why does my jQuery script only execute once?

I have the following jQuery script in my page, but it is only executing once, after the page is initially loaded. I'm trying to check the value of a radiobuttonlist control, each time it is clicked. But its only happening when the page is loaded initially.

        $(document).ready(function () {
            $('.rblReqType input').click(function () {
                var thisVal = $(this).val();
                if (thisVal == '2') {
                    alert('TEST');
                    $("[id$=spnDept]").text('Department');
                }
            });
        });

Open in new window


here is a snippet of my HTML

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:Panel ID="pnlReturnToList" runat="server">
    </asp:Panel>
    <div class="clear" />
    <asp:ScriptManager runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <br />
            <table align="center" cellpadding="0" cellspacing="0" class="style14">

		.
		.
		.
		                                        <td style="vertical-align: bottom; float: left;">
                                            <asp:RadioButtonList ID="ddlRequestType1" runat="server" CssClass="rblReqType" RepeatDirection="Horizontal"
                                                OnSelectedIndexChanged="ddlRequestType1_SelectedIndexChanged" AutoPostBack="true">
                                                <asp:ListItem Selected="True" Text="ADD" Value="0" />
                                                <asp:ListItem Text="CHANGE" Value="1" />
                                                <asp:ListItem Text="DEACTIVATE" Value="2" />
                                            </asp:RadioButtonList>
                                        </td>

Open in new window

0
Michael Sterling
Asked:
Michael Sterling
  • 7
  • 3
1 Solution
 
GaryCommented:
Are these radio buttons being reloaded via ajax?
0
 
leakim971PluritechnicianCommented:
replace :
$('.rblReqType input').click(function () {
by (if you've jQuery version greater or equal to 1.9) :
$('#<%= UpdatePanel1.ClientID %>").on("click", ".rblReqType input", function () {
or by by (if you've jQuery version lower than 1.9) :
$('.rblReqType input').live("click", function () {

because you've an update panel which replace(new content loaded with ajax) the radio buttons with new one and their bind events attached
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Gary: no.

@leakim971: ok, that worked, but it didn't. what i mean is, it changed the value but then quickly erased it. I need the value to stick.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Just to add some more detail, now, even going back to the original way I had it written, it is still "flickering" the value in and out. It wasn't doing that before, with the way i had it written originally.
0
 
leakim971PluritechnicianCommented:
so don't use an update panel or use :

$('#<%= UpdatePanel1.ClientID %>").on("click", ".rblReqType input", function (event) {
    event.preventDefault();
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@leakim971: while, that is causing me to not be able to select either of the other two radial buttons. it does display the message in my span, but can't choose any other radial buttons.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
@leakim971: what i described above happens in Firefox. in IE it displays the alert message, flickers my changed text, erases my changed text and default the radiobuttonlist back to its default selection, which is the first radial button.
0
 
leakim971PluritechnicianCommented:
I have not solution for you, you should rethink your application IMHO but wait other experts advice
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Thank you. I'll continue to look and wait.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
I solved it but not using jQuery, so i'm going to post my solution and see what happens.

I just added runat="server" to my span tag and changed it when the radiobuttonlist value changed.

my HTML
                                        <td style="vertical-align: bottom; float: left;">
                                            <asp:RadioButtonList ID="ddlRequestType1" runat="server" CssClass="rblReqType" RepeatDirection="Horizontal"
                                                OnSelectedIndexChanged="ddlRequestType1_SelectedIndexChanged" AutoPostBack="true">
                                                <asp:ListItem Text="ADD" Value="0" />
                                                <asp:ListItem Text="CHANGE" Value="1" />
                                                <asp:ListItem Text="DEACTIVATE" Value="2" />
                                            </asp:RadioButtonList>
                                        </td>
					.
					.
					.
					<span id="spnDept" style="color: Red;" runat="server">*</span>

Open in new window


my C# code behind
    protected void ddlRequestType1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((ddlRequestType1.SelectedIndex == 0))
        {
            spnDept.InnerText = "*";
            lblHrDtTermDt.Text = "Hire Date:";
        }
        else if (ddlRequestType1.SelectedIndex == 1)
        {
            spnDept.InnerText = "*";
            lblHrDtTermDt.Text = "Change Date:";
        }
        else if (ddlRequestType1.SelectedIndex == 2)
        {
            spnDept.InnerText = "";
            lblHrDtTermDt.Text = "Termination Date:";
        }
    }

Open in new window

0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
really wanted to solve this with jQuery. but i guess "there's more than one way to skin a cat...". no offense to animal lovers. i love dogs (all animals really :-) )
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now