0

I am developing web site using jQuery and other tech. I have problem in selector as below.

My page contains a div with id='tblData'. Inside #tblData I have created multiple tables with class='data'.

Inside a table (.data), i have created multiple tr and td. Some of these td have class='vis'.

I am changing display property of these td.vis dynamically from block to none as per my requirement.

When page loads I want to loop through all elements. My condition is that I want to loop through all td's inside table (with class='data') whose display property is not none.

I am writing following code but it doesn't work.

$("div#tblData .data td[class='vis']").each(function (i) {  
    if ($(this).attr('display') != "none") {
        if ((i % 2) == 0) {
            $(this).removeClass("comparecontent2").removeClass("comparecontent1").addClass("comparecontent2");  
        } else {
            $(this).removeClass("comparecontent2").removeClass("comparecontent1").addClass("comparecontent1"); 
        }
    }
});  

alert($("div#tblData .data td[class='vis']").size()); // shows zero while 'alert($("div#tblData .data td").size())';
                                                      // returns right count.
2
  • Tds should not be set to display block Commented Oct 10, 2011 at 7:57
  • initially td's display property is not set and later its changes to none Commented Oct 10, 2011 at 9:05

2 Answers 2

1

Use one of those two commands:

.is(":hidden")
.filter(":hidden")

http://api.jquery.com/hidden-selector/

e.g.

  $(this).is(":hidden")

or

  $("td:hidden")
Sign up to request clarification or add additional context in comments.

2 Comments

You're missing the colon on the selector in the is example. The way you have it, it's looking to see if it's a hidden element (e.g., like checking if it's a div element). Edit: Ah, you've fixed it now.
Yeah I just came out of my bed ;)
1

"display" isn't an attribute, it's a style property. You could use this.style.display, but you may want jQuery's :visible selector instead. Note that the :visible selector checks things other than just the display style property, but they're mostly things you probably want to check. Has more overhead, but unless you're in a really tight loop that probably doesn't matter.

So either:

if (this.style.display != "none")  

Or:

if ($(this).is(':visible'))  

Comments

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.