Solved

Why does my jQuery script only execute once?

Posted on 2014-10-01
11
113 Views
Last Modified: 2014-10-06
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
Comment
Question by:mikesExpertExchange
  • 7
  • 3
11 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40355206
Are these radio buttons being reloaded via ajax?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40355216
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
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 40355228
@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
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 40355238
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
 
LVL 82

Expert Comment

by:leakim971
ID: 40355254
so don't use an update panel or use :

$('#<%= UpdatePanel1.ClientID %>").on("click", ".rblReqType input", function (event) {
    event.preventDefault();
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 40355262
@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
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 40355269
@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
 
LVL 82

Expert Comment

by:leakim971
ID: 40355279
I have not solution for you, you should rethink your application IMHO but wait other experts advice
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 40355284
Thank you. I'll continue to look and wait.
0
 
LVL 1

Accepted Solution

by:
mikesExpertExchange earned 0 total points
ID: 40355315
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
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 40363273
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now