how to use jquery to hide or show label and textbox dep on a listbox selection?

Hi, I'm using asp.net 3.5 and C#
lbOtherExercise, listbox, allows multiple selection.  If one of the selected value is 11 then I would like to show the label and textbox otherwise, hide them.  Attached are my code.  Thank you.
ManageAccounts.aspx
lapuccaAsked:
Who is Participating?
 
khan_webguruCommented:
I think above code will not work with multi selection as well as if you will directly click on last "Exercise" value first time before selecting any thing else it will not work but not sure. I download your code and tried to implement this.

One this you already apply onchange method on this multi seletion that was postback the page to stop this I remove the onchange="" from your code.

TO GET YOUR DESIRE RESULT

Please copy paste this code in ready method means right after this line

 
$(document).ready(function () {

Open in new window


Add this piece of code

 
$("#lsAPExerciseOptions").click(function () {

                var realvalues = [];
                var textvalues = [];
                var found = false;
                $('#lsAPExerciseOptions :selected').each(function (i, selected) {
                    if (parseInt($(selected).val()) == 11) {
                        found = true;
                    }
                });

                if (found) {
                   
                    $("lbOtherExercise").show();
                    $("#txtOtherExercise").show();
                }
                else {

                    $("lbOtherExercise").hide();
                    $("#txtOtherExercise").hide();
                }

            });

Open in new window


In this code on selection I looked into all selected values if value will be 11 then that will show text box other wise that will hide that.

If you want to hide this textbox on page load time then just add this line in ready method

 
$("#txtOtherExercise").hide();

Open in new window


Hope this will help you.

Regards,

Asif Ahmed Khan
0
 
Gurvinder Pal SinghCommented:
@lapucca: could you please share a HTML only version of your problem?
0
 
lapuccaAuthor Commented:
Yeah, that is a good idea.  Thanks.
htmlCode.htm
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Gurvinder Pal SinghCommented:
which specific textboxes do you want to show or hide based on listbox selections?
0
 
lapuccaAuthor Commented:
the control's id lbOtherExercise and txtOtherExercise.  Thank you.
0
 
Gurvinder Pal SinghCommented:
add this into your document ready event handler

                  $("#lsAPExerciseOptions").change(function(){
                        if ( parseInt($(this).val()) >= 11 )
                        {
                              $("lbOtherExercise").show();
                              $("#txtOtherExercise").show();
                        }
                        else
                        {
                              $("lbOtherExercise").hide();
                              $("#txtOtherExercise").hide();
                        }
                  });
0
 
lapuccaAuthor Commented:
Khan, you're correct.  I tried that code and exactly as you described.  Your script does work with multi-select.  Thank you both.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.