?
Solved

Why does my jQuery script only execute once?

Posted on 2014-10-01
11
Medium Priority
?
152 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:Michael Sterling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Michael Sterling
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
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.

 
LVL 1

Author Comment

by:Michael Sterling
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
 
LVL 1

Author Comment

by:Michael Sterling
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:Michael Sterling
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:Michael Sterling
ID: 40355284
Thank you. I'll continue to look and wait.
0
 
LVL 1

Accepted Solution

by:
Michael Sterling 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:Michael Sterling
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

765 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