Click here to Skip to main content
15,299,443 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to change the button text on its click alternatively using jquery on aspx page. I have used the below code for this. But the else part doesn't work.
Please help.


C#
$(function () {
                $("#<%=btnSearch.ClientID %>").click(function () {
                    $("#dv").slideToggle(500);
                    if ($(this).text("Hide Selection Criteria")) {
                        $(this).html('Show Selection Criteria');
                        //alert("aaaa");
                    }
                    else {
                        if ($(this).val() = "Show Selection Criteria") {
                            $(this).html('Hide Selection Criteria');
                            alert("bbbb");
                        }
                    }
                    return false;
                });
            });
Posted

C#
var x = 1;
           $(function () {
               $("#<%=btnSearch.ClientID %>").click(function () {
                   $("#dv").slideToggle(500);
                   if ((x % 2) != 0) {
                       $(this).text('Show Selection Criteria');
                       x++;
                   }
                   else {
                       $(this).html('Hide Selection Criteria');
                       x++;
                   }
                   return false;
               });
           });
   
Try this code.
Use $(this).val() instead of if ($(this).text("Hide Selection Criteria")

JavaScript
$(function () {
                $("#<%=btnSearch.ClientID %>").click(function () {
                    $("#dv").slideToggle(500);
                    if ($(this).val() == "Hide Selection Criteria") {
                        $(this).html('Show Selection Criteria');
                        //alert("aaaa");
                    }
                    else if ($(this).val() == "Show Selection Criteria") {
                            $(this).html('Hide Selection Criteria');
                            alert("bbbb");
                    }
                    return false;
                });
            });
   
Comments
Member77 27-Mar-12 2:17am
   
now I have used below code even then the else part not working.

$(function () {
$("#<%=btnSearch.ClientID %>").click(function () {
$("#dv").slideToggle(500);
if ($(this).val("Hide Selection Criteria")) {
$(this).html('Show Selection Criteria');
}
else {
$(this).html('Hide Selection Criteria');
}
return false;
});
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900