Why background-color property is not working?

Hi there

this is my jsfiddle

I am trying to add background-color on newsBlock but background color of that block is not changing

What is wrong?

errrm, what did you expect display: none to do?

I don’t want Initially display the block but I want color of that block should be as I assign to it.

Have you tried changing it to display: block ?


var e=$(".newsBlock").attr("style", "display: block");

I suspect there is something wrong with the logic in your JS.

I’m moving this to the JavaScript category.

1 Like

A display:none’d element will remove all aspects of it. Background color included. Looks like you want to remove the children, and not the newsblock element itself.

display:none reduces its size to zero.

To keep the space there but hide the content requires visibility:hidden.

see my updated code

doing display:block is adding just only
adding one more extra block other than looping condition

some more help on this topic anyone?

vngx, I would appreciate more context. I do not understand what you are trying to do and under what conditions the JS is triggered. I just don’t get the bigger picture; therefore, do not understand this smaller picture.

Can you provide a link to the web page under development?

Thanks for reply

my task is very simple I want to add the newsBlock to listNews 3 times
underloop condition

If I display it at start then the block is adding 4 times I want that block only add 3 times and background-color is red

I hope now its clear

No, it is not clear to me.

Are you wanting to use the initial newsBlock section as a template, and to not have that showing? Using that template to add new sections to the page?

An update to your jsfiddle seems to be closer to what you’re wanting to achieve, where a template section is used, removed from the page (detached), and then cloned to create the new sections.

1 Like


this is exactly I was looking for

thanks for template Idea

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.