-5

I have a class on a div ($('#cover-image-item')) that has:

display: none;

In my js I have:

$('#cover-image-item').css( "display", "block");
$('#cover-image-item').show();

The div is not shown when the js code is ran, where am I going wrong?

9
  • 4
    can you create a jsfiddle? Sep 20, 2013 at 15:41
  • This code should work, can you create an example to show your issue?
    – gen_Eric
    Sep 20, 2013 at 15:42
  • 6
    .show() will update the display property for you, there is no need to change it first. Sep 20, 2013 at 15:42
  • 3
    @panthro You'll need to show more code then. The code you posted here should work. Sep 20, 2013 at 15:47
  • 2
    @panthro Something obviously isn't irrelevant if this isn't working, because it should. Try recreating the issue in a jsfiddle.net.
    – ajp15243
    Sep 20, 2013 at 15:48

6 Answers 6

3

If cover-image-item is indeed a class and not an ID, you need to use $('.cover-image-item') not $('#cover-image-item').

As others have mentioned, there's no need to update the display value, as that gets updated by show()

2

The # means "id" attribute, not class. Made sure your div is this:

<div id="cover-image-item" />

If it is this...

<div class="cover-image-item" />

...change your jQuery selector to class:

$('.cover-image-item').show();

1
  • Can you post your HTML for the "cover-image-item" <div>?
    – utt73
    Sep 20, 2013 at 15:52
0

I needed to remove the class attached to the div.

$('#cover-image-item').removeClass('hidden');

1
  • Can you show your relevant markup for the <div> in your question so that this has better context?
    – ajp15243
    Sep 20, 2013 at 15:51
0

Don't write .css("display","block")

-1

remove the .css("display, "block");

1
  • if it is a class, you must use $('.cover-image-item').show(); not $('#cover-image-item').show();
    – Moe Assaad
    Sep 20, 2013 at 15:47
-2

The .css( "display", "block"); is unnecessary and possibly causes your problem.

7
  • 3
    I agree that it is unneccessary but I don't see how it can cause it to NOT show
    – Huangism
    Sep 20, 2013 at 15:43
  • @Huangism if he is using the display otherwhise to position his element (inline-block for example) setting it to block can break his layout and possibly cause it to not show. It's safer to just use show() as it will revert the display to it's previous value. Sep 20, 2013 at 15:43
  • Layout might break but it will still show up somewhere. It's hard to say without seeing some more markup
    – Huangism
    Sep 20, 2013 at 15:44
  • Indeed. It's more likely his problem is caused by other conflicting code. Sep 20, 2013 at 15:46
  • not from me but carefully reading his question, he says class when he targeted id.
    – Huangism
    Sep 20, 2013 at 15:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.