1

I'm creating dynamically divs using javascript. I want to assign the style to none.

style="display: none;"

According to this question, I can to do this with the following options

 publishcontent.style.display="none";
 publishcontent.setAttribute("style", "display: none;");

But this isn't working. This is the way I created the divs. No way of the options I found is working. If I edit the html with firebug and type style="display: none;" it works.

This is a demo showing the example.

publishpaging=document.createElement("div");
var name="df"+counterName;
publishpaging.id=name;
counterName=counterName+1;
arrayNames.push(name);
counter++;
publishcontent=document.createElement("div");//"<div class='bonecardSmall'></div>";
publishcontent.className = "bonecardSmallP";
publishcontent.id=index.id;
if(arrayNames.length > 1){
    //publishpaging.style.display="none";
    publishpaging.setAttribute("style", "display: none;");
}
publishpaging.appendChild(publishcontent);
5
  • are you sure that the if condition is true Commented Aug 5, 2014 at 4:58
  • yes, it is inside a for, there's the jsbin showing the loop Commented Aug 5, 2014 at 5:00
  • I think you want to check arrayNames.length > 0 your jsbin is showing arrayNames.length is 1. So this condition will not be true. Commented Aug 5, 2014 at 5:03
  • I have a loop from 0 to 12. Each time I do arrayNames.push(name) the length will be greater that 1 after the first iteration. That's not the issue Commented Aug 5, 2014 at 5:05
  • you just want to hide it? jsbin.com/yibeqoqa/1/edit Commented Aug 5, 2014 at 5:09

4 Answers 4

5

"name" variable stores the id of publishcontent so use it in jquery

 $('#'+name).css('display','none');
Sign up to request clarification or add additional context in comments.

1 Comment

The OP already has a reference to the element as publishpaging, so the property can be set directly. The jQuery should be $(publishpaging).css('display','none') at least. However, that is very inefficient, there's no reason why publishcontent.style.display = 'none' should not work.
0

using jQuery, just use:

$('div').css('display','none');

2 Comments

it doesn't work $(publishcontent).css('display','none');
@Diego where is the newly added div appended? what is the id or class of the parent element?
0

Using jquery try this,

$('yourdivtagid').css('display','none')

1 Comment

No he just mentioned $('div'). I think he should use div tag id.
0

you can simply hide using jquery hide function.

$("#"+ name).hide();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.