show hide don't work with runtime variable

Dinesh Bali
Dinesh Bali used Ask the Experts™
on
Hi,

I wanted to show and hide div based on condition in jquery

Below code works and hide all div's having class "countryselection"
$(".countryselection").hide();

Open in new window


But if I am passing variable at runtime and show all div's like below:
$(selectedValue).show();

Open in new window


but it doesn't work.

Below is the code snippet. Class in variable "selectedValue" is already applied in the div.

Please advise.
var selectedValue = jQuery(this).val();
$(".countryselection").hide();

alert('vcountry: ' + selectedValue);
$(selectedValue).show();

Open in new window


Regards,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
so you mean first alert and second doesn't match right?
var selectedValue = jQuery(this).val();
alert('vcountry1: ' + selectedValue);

$(".countryselection").hide();

alert('vcountry2: ' + selectedValue);
$(selectedValue).show();

Open in new window

Author

Commented:
Hi

Thanks for your reply

Alert I have placed to see values only.

Everything is working expect last line

$(selectedValue).show()

I can see alert with correct value for variable selectedValue

alert('vcountry2: ' + selectedValue);
Senior Director of Technologyy
Commented:
What is in the variable selectedValue?  I am guessing a string that represents a classname. If so you would need to do $(‘.’ + selectedValue).show();

It would be helpful to understand what is the output of the alert statement.

Hope this helps

Author

Commented:
Many thanks.
Silly mistake from myside.
Thanks again for pointing it out.

Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial