0

I'm far from a JavaScript expert but I'm trying to migrate some mootools code to jQuery but am struggling!

I have a web form which contains several tables with a css class of '.table-row'. Upon clicking a button, I'd like to find all the rows which have '.table-row' and if their display property is set to none, I'd like to remove them from the page before submitting.

This is my mootools code:

function remove_hidden_elements() {
    var myElements = $$('.table-row');
    myElements.each(function (item, index) {
        var vis = item.getStyle('display');
        if (vis == 'none') {
            item.dispose();
        }
    });
}

Please can someone point me in the right direction for achieving this within jQuery?

Many thanks in advance.

5
  • Can you provide an HTML snippet you are working with? It's unclear whether the rows or the tables have that .table-row class Commented Jul 11, 2011 at 21:45
  • 1
    So far it doesn't look like you have read the basic documentation at the jquery website. Start with the "How Jquery Works" article and the "Getting Started" tutorial. That will answer a lot of your questions. Commented Jul 11, 2011 at 21:47
  • @spliter - it's the rows that have the class. Sorry, I should have been more explicit in my post. Commented Jul 12, 2011 at 11:35
  • @Cos Callis - You're right, I should definitely read through again and get a thorough understanding. I have read some documentation but I was struggling quite a bit so thought I'd ask. Commented Jul 12, 2011 at 11:37
  • Thanks to everyone who replied. It's very much appreciated. Commented Jul 12, 2011 at 11:38

3 Answers 3

2

jQuery and MooTools aren't really all that far apart; jQuery is more set-oriented and MooTools more element-orientated, but other than that...

The literal jQuery equivalent is:

function remove_hidden_elements() {
    var myElements = $('.table-row');
    myElements.each(function() {
        if (this.style.display === 'none') {
            $(this).remove();
        }
    });
}

Basically, swapping $$() for $() (aka jQuery()) and using the raw DOM element that jQuery will pass into its version of each to check the style.display property, and then using remove (which will also find and destroy any event handlers or other associated data, provided they were hooked up / associated via jQuery).

Somewhat more idiomatic jQuery might be:

function remove_hidden_elements() {
    var myElements = $('.table-row');
    myElements.each(function() {
        var $this = $(this);
        if ($this.is(":not(:visible)")) {
            $this.remove();
        }
    });
}

Or even eliminating the variable:

function remove_hidden_elements() {
    $('.table-row').each(function() {
        var $this = $(this);
        if ($this.is(":not(:visible)")) {
            $this.remove();
        }
    });
}

jQuery's :visible selector handles several cases other than the literal style.display == 'none', and :not inverts it. (Or rather more directly, use :hidden as 3nigma points out — I must be tired.) But if you specifically want to handle just that case of style.display == 'none' (and there's nothing wrong with that, it can be more efficient), use the first example above (with or without the myElements variable).

Sign up to request clarification or add additional context in comments.

3 Comments

Do you mean if ($this.not(':visible')) ? You're removing only the visible elements at the moment...
@Beejamin: Thank you, yes, I had inverted that logic, hadn't I?
Many thanks, T.J. This is exactly what I was looking for. Works a treat.
2

you can use the :hidden selector

$(".table-row:hidden").remove();

Comments

1

Use :not and :visible selectors to filter the rows.

Try this:

function remove_hidden_elements() {
  $('.table-row:not(:visible)').remove();
}

Note:

As per jQuery docs

Elements can be considered hidden for several reasons:

•They have a CSS display value of none.  

•They are form elements with type="hidden".  

•Their width and height are explicitly set to 0.  

•An ancestor element is hidden, so the element is not shown on the page.  

If you are strictly looking for disaply:none filter then try:

function remove_hidden_elements() {
  $('.table-row').filter(function(){
    return (this.style.display === "none");
  }).remove();
}

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.