0

I have a div in which there is criterion to apply different class. For example if in loop i is odd then .odd-product is applied to the div and if i is even then .even-product is applied to the div.

Here it is:

<div id="product"<?php if($i%2==0){?>class="even-product"<?php } else {?>class="odd-product"<?php } ?>>

Now I reference that div in the following way in jQuery:

    $divClass=$('#product').attr("class");

Now I want to hide a div whose class name is contained in $divClass variable. How to do that?

0

1 Answer 1

3

You would just use it like a string:

  $divClass=$('#product').attr("class");
  $("." + $divClass).hide();
Sign up to request clarification or add additional context in comments.

4 Comments

why $ is used here @J Torres rether then var.
OP named his variable "$divClass". I don't agree with it, I just went with it.
It's good practice to name jQuery object variables with the $ to indicate that those variables are, in fact, jQuery objects. The $ does not affect the behavior of the variable in any way.
Right, but $('#product').attr("class") isn't a jQuery object, it's a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.