Solved

Why does my jQuery script only execute once?

Posted on 2014-10-01
11
134 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jquery tab header text 1 23
JQuery Autocomplete Tag AJAX (Need nice script) 11 35
JQuery Syntax... 4 37
googleapis - jQuery.com and so forth 3 14
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

839 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